I am trying to subclass UIBarButtonItem to add some special functionality. I need the barButtonItem to toggle its appearance when touched, and am therefore attempting to override performSelector:.
When I use the code below, I get an EXC_BAD_ACCESS (code=2 ...)
-(id)performSelector:(SEL)aSelector withObject:(id)object1 withObject:(id)object2
{
// Do something
return [super performSelector:aSelector withObject:object1 withObject:object2];
}
My guess is that I am either wrongly attempting to override performSelector: (is there another way?) or incorrectly calling the method on super.
After searching for a solution for 3+ hours I have found nothing. Any help is greatly appreciated.
Update:
The following works:
@implementation CustomBarButtonItem
- (void)setTarget:(id)target
{
_realTarget = target;
super.target = self;
}
- (void)setAction:(SEL)action
{
_realAction = action;
super.action = @selector(pressed);
}
- (void)pressed
{
[self doCustom]; // implement this somewhere
[_realTarget performSelector:_realAction withObject:nil afterDelay:0];
}
Unfortunately I wanted to toggle between having a customView and the normal UIBarButtonItem look by setting self.customView = nil which only works sometimes. But that is a-whole-nother question. Thanks all.
I will wait a little longer to choose a best answer to see if there are better solutions.
Probably better to implement this functionality using a simple
UIViewthat you can simply modify in any given way.UIBarButtonItemdoes provide a way for initializing based on a custom UIView:- (id)initWithCustomView:(UIView *)customViewYou could then tell the
UIViewto change its appearance based on the touch events fired by theUIBarButtonItem.