Reputation: 68840
When I push a new ViewController onto a navigation controller stack the "back" button is the Title of the previous controller.
How can I change the text in the back button to "Back" instead of the default name-of-last-controller?
Upvotes: 8
Views: 19110
Reputation:
You can achieve this by setting the Back button title in the originating controller's "viewWillDisappear" function as follows:
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
//Set Title for this view
self.navigationItem.title = "My Title"
}
override func viewWillDisappear(animated: Bool) {
super.viewWillAppear(animated)
//Set Title for back button in next view
self.navigationItem.title = "Back"
}
Upvotes: 1
Reputation: 838
I know, the question is very old, but I found a nice solution.
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] init];
barButton.title = @"back";
self.navigationController.navigationBar.topItem.backBarButtonItem = barButton;
Works from childView! Tested with iOS 7.
Upvotes: 13
Reputation: 2822
You can change the title of the current view controller of the navigation controller before push the new view controller:
self.title = @"Custom Title";
[self pushViewController: newViewController ...];
and in the navigation controller's delegate class
-(void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
if([viewController class] == [OldViewController class]) {
viewController.title = @"Your previous title";
}
}
Upvotes: 2
Reputation: 1265
This is the proper way to make a back button with something other than the previous' pages title
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] init];
barButton.title = @"Your Title";
self.navigationItem.backBarButtonItem = barButton;
Its to my understanding that:
self.navigationItem.backBarButtonItem.title = @"Your Title";
will make the Title of the navigation bar on the previous page this which is not what you want.
Upvotes: 27
Reputation: 603
You can actually set the title on the main view controller's navigationItem
's title. Basically each UIViewController
has a little stub UINavigationItem
which contains metadata about how that view should be referenced inside a UINavigationController
. By default, that metadata just falls back to the UIViewController
itself.
Assuming 'self' is the UIViewController
of the view that's visible inside the UINavigationController
, set:
self.navigationItem.title = @"My Custom Title"
Upvotes: 1
Reputation: 42542
You need to create a custom button on the navigation controller. Put the following code in the viewDidLoad in your Root View Controller:
UIBarButtonItem * tempButtonItem = [[[ UIBarButtonItem alloc] init] autorelease];
tempButtonItem .title = @"Back";
self.navigationItem.backBarButtonItem = tempButtonItem ;
By setting the navigation bar button on the Root View Controller, the pushed view shows the appropriate back button.
Upvotes: 10