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 back button pulls its text from the title of the parent view controller.
In the parent view controller (the view controller that appears when you tap the back button), set its own title as the desired text on the back button.
For example, let's say we have a RootViewController
class. When we click a cell in its table view, we push an instance of SecondViewController
. We want the back button of the SecondViewController
instance to read, "Home."
in the viewDidLoad
method of RootViewController.m:
self.title = @"Home";
in the viewDidLoad
method of SecondViewController.m:
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.backBarButtonItem = backButton;
[backButton release];
If you want your back button to read, "Back," set the title of the parent view controller to @"Back"
;
As a previous poster (Brenden) mentioned, by default the value of backBarButtonItem is nil, so setting its title results in sending a message to a nil object which of course gets ignored.
If you don't want to muck around with manually creating a new button in order to set its title, there is a work around in Interface Builder.
In IB, for a given view controller that's part of a navigationController stack, you can click on the navigationItem representing the navigationBar. When you click, the properties inspector lets you set three properties: Title, Prompt and BackButton.
If, in the backButton field you type in any old random text, this has the effect of instantiating a backBarButtonItem object and in your code, you are now able to set its title to any text you want.
Hope this helps someone.
I have interesting solution:
In the method that navigate to the second view (IBAction or didSelectRaw etc.) I change to the title that I want in the title of back item, for example: self.title = @"Inbox";
. In addition, I add method ViewWillApear to change the title when the user click back to primary title.
- (void)pushViewController:(UIViewController *)controller
withBackTitle:(NSString *)backTitle
animated:(BOOL)animated {
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:backTitle
style:UIBarButtonItemStyleDone
target:nil
action:nil];
[self.navigationController pushViewController:controller animated:animated];
}
it is works.
The answers by Mihai Damian and Chris Lundie are correct.
Another thing to note however is that if you change your viewController's navigationItem.title after having set your custom navigationItem.backBarButtonItem, it will go back to its default state, which is to discard your custom backBarButtonItem and instead use the new title you just set.
So, I ensure that I re-set the backBarButtonItem whenever I change the viewController's title:
self.navigationItem.title = @"New Title";
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] init];
backButton.title = @"Back";
self.navigationItem.backBarButtonItem = backButton;
self.navigationController.navigationBar.backItem.title = @"TEXT";