我正在尝试为 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];
}
根据我读过的内容,它应该可以工作-我做错了什么?
实际上,这是解决我不理解的更复杂的事情。
我想从 UINavigation Controller 中的当前 UIViewController 发送一条消息。
UINavigationController 有这个按钮
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btnmenu.png"] style:UIBarButtonItemStylePlain target:self.navigationController.parentViewController action:@selector(revealToggle:)];
效果很好。我真正要做的是
[self.navigationController.parentViewController closeToggle];
从 didSelectRowAtIndexPath 函数中失败,但我设法用 [theButton.target performSelector:theButton.action] 实现了相同的效果