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?

Ian Vink
  • 66,960
  • 104
  • 341
  • 555
  • Just know that this is frowned upon by Apple's iPhone interface guidelines. (Though admittedly needed when titles run long.) – Johan Kool Jan 13 '10 at 00:17

6 Answers6

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.

Matt
  • 1,265
  • 1
  • 16
  • 24
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.

seniorbenelli
  • 838
  • 1
  • 9
  • 20
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.

RedBlueThing
  • 42,006
  • 17
  • 96
  • 122
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";

}

}
Ashish Awaghad
  • 2,822
  • 3
  • 24
  • 33
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"
alecf
  • 603
  • 1
  • 4
  • 10
  • 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 '10 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"
}
Tunaki
  • 132,869
  • 46
  • 340
  • 423
John Carto
  • 165
  • 1
  • 4