Question

I'm changing the back button item title in the viewDidAppear of a controller in the following way:

self.navigationController.navigationBar.backItem.title = @"Previous";

It changes the tittle properly, but the I'm having a strange behaviour. When I select the "previous" button, it changes the tittle of the controller that is up in the stack (i.e the parent controller now has the title "Previous".

Do you now why this happened?

Was it helpful?

Solution

When you're using a navigation controller, calling [self setTitle:@"Title"]; inside of any view controller in the stack will set the navigation bar title. This is also the title used by default for the back button when you've pushed a new view controller. Apparently, from what you are experiencing, explicitly setting the title of the backItem, also sets it for the navigation bar title for the previous view controller overriding whatever what specified in the call to -setTitle in the view controller.

You will probably be better off just managing the title from within the view controllers in your navigation stack. When you go to push a new view controller, do this:

[self setTitle:@"Previous"];
NextViewController *controller = [[NextViewController alloc] init];
[[self navigationController] pushViewController:controller animated:YES];
[controller release], controller = nil;

Now, when the next view controller displays, the back button with say "Previous". Now, you just need to change it back to whatever its real title should be in -viewWillAppear:

- (void)viewWillAppear:(BOOL)animated;
{
    [self setTitle:@"Real Title"];
    [super viewWillAppear:animated];
}

It may feel a little hacky, but it's better than trying to override the navigation bar functionality. Wrestling with the nav bar/nav controller stack can prove very frustrating.

Best regards.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top