0

我在我的应用程序中使用 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;

}

这工作正常。但是,当我使用搜索控制器并退出它时,我的右上角按钮突然变为取消按钮(见下图)。我怎样才能解决这个问题?提前致谢。

在此处输入图像描述

4

2 回答 2

1

好的,刚刚想出了如何解决这个问题。我刚刚添加了一个通知以查看何时隐藏键盘并将按钮添加回导航栏。

1)声明并综合属性mainNavigationController:

@property UINavigationController *mainNavigationController;

2) 在 (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated 中,添加:

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;
}
于 2013-05-14T02:16:58.223 回答
0

要解决此问题,您必须实现<UISearchDisplayDelegate>and 在navigationController:willShowViewController:animated:方法中将 searchDisplayController 的委托设置为 selfviewController.searchDisplayController.delegate = self;

于 2014-04-24T23:39:06.327 回答