[英]iOS: Removed cancel button reappears after using search controller
I am using an ABPeoplePickerNavigationController in my app and have overrode the navigation bar buttons to my own using UINavigationControllerDelegate. 我在我的应用程序中使用ABPeoplePickerNavigationController,并使用UINavigationControllerDelegate将导航栏按钮改写为我自己的按钮。
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
navigationController.topViewController.searchDisplayController.searchBar.barStyle = UIBarStyleBlack;
navigationController.topViewController.navigationItem.leftBarButtonItem = nil;
navigationController.topViewController.navigationItem.rightBarButtonItem = nil;
UIBarButtonItem *cancelButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
target:self
action:@selector(cancel:)];
navigationController.topViewController.navigationItem.leftBarButtonItem = cancelButtonItem;
UIBarButtonItem *addButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:@selector(addItem:)];
navigationController.topViewController.navigationItem.rightBarButtonItem = addButtonItem;
}
This works fine. 这很好。 However, when I use the search controller and exit out of it, my top right button suddenly changes to a Cancel button (see image below).
但是,当我使用搜索控制器退出其中时,右上角的按钮突然变为“取消”按钮(请参见下图)。 How can I fix this?
我怎样才能解决这个问题? Thanks in advance.
提前致谢。
OK, just figured out how to fix this. 好的,只是想出了解决方法。 I just added a notification to see when the keyboard will be hidden and added the button back to the navigation bar.
我刚刚添加了一条通知,以查看何时隐藏键盘,并将按钮添加回导航栏。
1) Declare and synthesize the property mainNavigationController: 1)声明并综合属性mainNavigationController:
@property UINavigationController *mainNavigationController;
2) In (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated, add: 2)在(void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController动画化:(BOOL)动画中,添加:
mainNavigationController = navigationController;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onNotification:) name:UIKeyboardWillHideNotification object:nil];
3) Add the method onNotification: 3)添加onNotification方法:
-(void)onNotification:(NSNotification*)notification
{
UIBarButtonItem *addButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:@selector(addItem:)];
mainNavigationController.topViewController.navigationItem.rightBarButtonItem = addButtonItem;
}
要解决此问题,您必须实现<UISearchDisplayDelegate>
并在navigationController:willShowViewController:animated:
方法中,将searchDisplayController的委托设置为self viewController.searchDisplayController.delegate = self;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.