2
votes

As the question says, I need to be able to change the contents of the UINavigationBar when different UIViewControllers are displayed by the UINavigationController. This is easily achievable for titles since the navigation controller just uses the view controller's title property. What I am struggling with is changing the left and right bar button items depending on the view controller.

The code that I would use to change them has already been addressed in multiple other questions and isn't really the issue, although for reference I will be using the following:

UIBarButtonItem *newItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:
    UIBarButtonSystemItemAdd target:self action:nil];
newItem.tintColor = [UIColor whiteColor];
self.navigationItem.rightBarButtonItem = newItem;

I think what I am looking for is a method which is called every time that a new UIViewController is shown by the navigation controller in order to set up the navigation controller layout. In the documentation of the UINavigationController is the following method:

// Called when the navigation controller shows a new top view controller via a push, pop or setting of the view controller stack.
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated;

However, this method is seemingly not called at any point when I run the app. So, is there a method which is called when a new view controller is shown by the navigation controller? Or am I going completely wrong here and there is already an accepted way of having the navigation bar change with the view being displayed beneath it which I have so far missed?

1
That method is part of the UINavigationControllerDelegate protocol, not the UINavigationController itself. Have you adopted the protocol and set the delegate? - pbasdf
In what way do you want to change the navigation bar that doesn't happen automatically? - Wain
Normally you assign the barButton items in each view controller individually (with a back button added automatically). Is there a reason why you want to do it in the navigation controller? - Joe Benton
@pbasdf, I was under the impression that the delegate was automatically set to the class which was controlling the uinavigationcontroller, this could well be where I am going wrong. Wain, I don't want the title of the previous uiviewcontroller in the top left space and I want to add in the various images such as the UIBarButtonSystemItemAdd to the top right space depending on the view controller. Joe Benton, this also could be where I am going wrong! Is it better to put the code I quoted in each controller's ViewDidLoad method? - Jack Schofield

1 Answers

2
votes

The typical solution is to put code in each view controller's viewDidLoad method that sets up the desired buttons.

The first bit of code in your question would be a perfect example of a view controller setting up a desired button to be shown on the right side of the nav bar when it (the view controller) is displayed.

There is nothing you need to do with navigation controller delegates or the navigation controller itself.