I’m trying to swop the action for a leftBarButtonItem
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIBarButtonItem *theButton = self.navigationItem.leftBarButtonItem;
// 'UIBarButtonItem' may not respond to 'removeTarget:action:forControlEvents:'
[theButton removeTarget:self action:@selector(revealToggle:) forControlEvents:UIControlEventTouchUpInside];
// 'UIBarButtonItem' may not respond to 'removeTarget:action:forControlEvents:'
[theButton addTarget:self action:@selector(closeToggle:) forControlEvents:UIControlEventTouchUpInside];
// BUT THIS DOES WORK
[theButton.target performSelector:theButton.action];
}
According to what I’ve read it should work – what am I doing wrong?
Actually this is a work around to something more complicated that I don’t understand.
I want to send a message from the current UIViewController that’s within the UINavigation Controller.
The UINavigationController has this button
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btnmenu.png"] style:UIBarButtonItemStylePlain target:self.navigationController.parentViewController action:@selector(revealToggle:)];
which works fine. What I really what to do is
[self.navigationController.parentViewController closeToggle];
from within the didSelectRowAtIndexPath function, which fails, but I managed to achieve the same effect with [theButton.target performSelector:theButton.action]
As I said in the comment,
UIBarButtonItemdoes not inheritUIControlmethods, thus your warnings foraddTargetandremoveTarget. Instead what you do is directly assign a newUIBarButtonItem:Also, notice the difference in your defined selector for the targeted
UIBarButtonItemcloseToggle: . But what you said you want to do is send a message to the parent controller of, closeToggle. The colon there is quite different as one expects a parameter. Performing an action oncloseToggle:if your method iscloseTogglewill result in a crash, and vice versa.Decide what method it is you want and follow the above signing of a new
UIBarButtonItem.Hope this helps.