iPhone: NavigationController NavigationBar Back button text
Asked Answered
R

6

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?

Ronel answered 13/1, 2010 at 0:9 Comment(1)
Just know that this is frowned upon by Apple's iPhone interface guidelines. (Though admittedly needed when titles run long.)Evangelize
O
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"
Osprey answered 13/1, 2010 at 0:22 Comment(1)
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".Betulaceous
B
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.

Betulaceous answered 17/11, 2010 at 18:9 Comment(0)
S
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.

Serenata answered 2/4, 2014 at 11:20 Comment(0)
F
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.

Foison answered 13/1, 2010 at 0:14 Comment(0)
D
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";

}

}
Dukas answered 3/1, 2011 at 2:57 Comment(0)
O
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"
Osprey answered 13/1, 2010 at 0:22 Comment(1)
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".Betulaceous
W
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"
}
Witham answered 10/9, 2016 at 19:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.