I have a navigationController-based app. I want to change the title of the back button for the root view controller. I have tried the following code in the rootViewControlle
The title of the back button is either:
If you are setting the back button for the current view controller's navigation item you are not setting the button that get's displayed in the current view. You are in fact setting the back button that will be used if you push another view controller from it.
Assumption: In your navigation controller have two ViewController is RootViewController and DetailViewController.
First Step: You must override back title of RootViewController
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"backTitle" style:UIBarButtonItemStylePlain target:nil action:nil];
Second Step:
[self.navigationController pushViewController:detailVC animated:YES];
That's done! You will see Back Button of DetailViewController is "backTitle"
You can't simply change the title of the back button in the root view controller because the root view controller is not displaying a back button. It is the root after all, what can you go back to? While there might be something logical in your app, there is nothing obvious the default implementation should do.
You can place a custom button there instead of you really want want a control there (make a UIBarButtonItem and set navigationItem.backBarButtonItem to it), though that will not have the same appearance as one of the default ones (it will be a square, as opposed to an arrow).
Your problem is probably very similar to the one described here: http://blog.tmro.net/2009/05/uitabbarbuttonitem-did-not-change-its.html
Try to debug to see if there are any _barButtonItemFlags set for your button...
Possibly already answered, but not simply.
self.navigationItem.backBarButtonItem
defaults to nil. So if you set the title to it, nothing will happen because it is affecting a nil
address object. This won't throw an error, because objective-c allows messaging nil
objects.
ANSWER: You must create your own button object in order to set it's title. You can initWithTitle
or create it and then set the title afterward.
PS - as mentioned, the backBarButtonItem
only affects it's child views that are pushed on top of it in the navigation stack. Depending on how you have your app architected, the root view can't be popped any further and can't have a back button. Though, you can affect the leftBarButtonItem
.
I've had success by creating my own UIBarButtonItem instead of setting the title of the existing one:
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.backBarButtonItem = backButton;
[backButton release];