8

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?

1
  • Just know that this is frowned upon by Apple's iPhone interface guidelines. (Though admittedly needed when titles run long.)
    – Johan Kool
    Jan 13, 2010 at 0:17

6 Answers 6

27

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.

13

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.

0
10

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.

2

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";

}

}
1

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"
1
  • 3
    This not only makes the buttons title "My Custom Title" but it also makes the previous page in the Navigation Stack's title "My Custom Title".
    – Matt
    Nov 19, 2010 at 15:09
1

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"
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.