Chun
Chun

Reputation: 1988

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.

- (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.

enter image description here

Upvotes: 0

Views: 466

Answers (2)

fruechtemuesli
fruechtemuesli

Reputation: 2791

To fix this you have to implement the <UISearchDisplayDelegate> and in the navigationController:willShowViewController:animated: method set the delegate of the searchDisplayController to self viewController.searchDisplayController.delegate = self;

Upvotes: 0

Chun
Chun

Reputation: 1988

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:

@property UINavigationController *mainNavigationController;

2) In (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) Add the method onNotification:

-(void)onNotification:(NSNotification*)notification
{
    UIBarButtonItem *addButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
                                                                                   target:self
                                                                                   action:@selector(addItem:)];
    mainNavigationController.topViewController.navigationItem.rightBarButtonItem = addButtonItem;
}

Upvotes: 1

Related Questions