我在我的应用程序中使用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;
}
这很好用。但是,当我使用搜索控制器退出时,我的右上方按钮突然变为“取消”按钮(见下图)。我怎样才能解决这个问题?提前谢谢。
答案 0 :(得分:1)
好的,刚想出如何解决这个问题。我刚刚添加了一个通知,以查看何时隐藏键盘并将按钮添加回导航栏。
1)声明并合成属性mainNavigationController:
@property UINavigationController *mainNavigationController;
2)在(void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated,add:
mainNavigationController = navigationController;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onNotification:) name:UIKeyboardWillHideNotification object:nil];
3)添加onNotification方法:
-(void)onNotification:(NSNotification*)notification
{
UIBarButtonItem *addButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:@selector(addItem:)];
mainNavigationController.topViewController.navigationItem.rightBarButtonItem = addButtonItem;
}
答案 1 :(得分:0)
要解决此问题,您必须实施<UISearchDisplayDelegate>
并在navigationController:willShowViewController:animated:
方法中将searchDisplayController的委托设置为自viewController.searchDisplayController.delegate = self;