Search code examples
iosuinavigationbaruinavigationitem

Is it possible to change the navigation bar title outside of viewDidLoad/viewWillAppear?


I'm trying to change the navigation bar title with this statement:

[[self navigationItem] setTitle:myTitle];

It works fine in viewDidLoad and viewWillAppear, but it doesn't work anywhere else. Is what I am trying to do possible? It's a view controller I'm working with.


Solution

  • There is a very simple way to change the title for the current view controller.

    self.title = @"New Title";
    

    Calling this on a view controller that is in a navigation controller will result in the title shown in the navigation bar being updated with the new title.

    This can be called at any time while the view controller is displayed.

    There is no need to dig into the view controller's navigationItem for setting the title.