30

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 rootViewController's viewDidLoad method, but no success:

self.navigationItem.backBarButtonItem.title = @"Back";

Any ideas?

Chris Hanson
  • 54,380
  • 8
  • 73
  • 102
Alpinista
  • 3,751
  • 5
  • 35
  • 46

14 Answers14

52

I've had success by creating my own UIBarButtonItem instead of setting the title of the existing one:

UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.backBarButtonItem = backButton;
[backButton release];
Chris Lundie
  • 6,023
  • 2
  • 27
  • 28
50

The title of the back button is either:

  1. The title of the previous view controller
  2. The name of the previous view controller's navigation item back button

If you are setting the back button for the current view controller's navigation item you are not setting the button that get's displayed in the current view. You are in fact setting the back button that will be used if you push another view controller from it.

Mihai Damian
  • 11,193
  • 11
  • 59
  • 81
  • 4
    This is something confusing but important. When setting the `Back Button` of the navigation item of a controller, you're actually setting the title for the back button displayed by a controller one level down the controller hierarchy, i.e., a controller pushed by the controller whose back button you're setting. – duci9y Oct 31 '13 at 15:56
  • Great! This is how you can change back button title after pushing UIViewController (2. The name of the previous view controller's navigation item back button). Thanx! – derpoliuk Mar 06 '14 at 14:35
  • This explains so much.. Never liked the UINavigationBar anyway – Yunus Nedim Mehel Dec 08 '14 at 14:21
27

Possibly already answered, but not simply.

self.navigationItem.backBarButtonItem defaults to nil. So if you set the title to it, nothing will happen because it is affecting a nil address object. This won't throw an error, because objective-c allows messaging nil objects.

ANSWER: You must create your own button object in order to set it's title. You can initWithTitle or create it and then set the title afterward.

PS - as mentioned, the backBarButtonItem only affects it's child views that are pushed on top of it in the navigation stack. Depending on how you have your app architected, the root view can't be popped any further and can't have a back button. Though, you can affect the leftBarButtonItem.

Brenden
  • 7,708
  • 11
  • 61
  • 75
5

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

Rose Perrone
  • 61,572
  • 58
  • 208
  • 243
1

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;
commanda
  • 4,841
  • 1
  • 25
  • 34
  • Remember to put this code in the viewDidLoad: of the view controller that is going to push the view controller with the back button item that has the title you want. Putting this code in the controller that is pushed won't work. – nemesys Sep 29 '12 at 06:51
1

You can't simply change the title of the back button in the root view controller because the root view controller is not displaying a back button. It is the root after all, what can you go back to? While there might be something logical in your app, there is nothing obvious the default implementation should do.

You can place a custom button there instead of you really want want a control there (make a UIBarButtonItem and set navigationItem.backBarButtonItem to it), though that will not have the same appearance as one of the default ones (it will be a square, as opposed to an arrow).

Louis Gerbarg
  • 43,356
  • 8
  • 80
  • 90
  • The root view does have a back button. It gets displayed when it is the 'previous' view on the stack. In other words you will see it when you push another view. Right? – Chris Lundie Jan 31 '09 at 22:50
  • No, when the rootViewController is the previous on the stack the default behavious is for the childViewControllers back button title to be set to its parents name. – Louis Gerbarg Feb 02 '09 at 01:12
0

Maybe you don't wan't every pushed viewController from a viewController have a Cancel button than just set a leftBarButtonItem in the display viewController with a custom title

self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"back", @"") style:UIBarButtonSystemItemCancel target:self action:@selector(popViewController)] autorelease];

- (void)popViewController {
    [self.navigationController popViewControllerAnimated:YES];
}
user1839842
  • 83
  • 1
  • 5
0

Assumption: In your navigation controller have two ViewController is RootViewController and DetailViewController.

  • First Step: You must override back title of RootViewController

    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"backTitle" style:UIBarButtonItemStylePlain target:nil action:nil];
    
  • Second Step:

    [self.navigationController pushViewController:detailVC animated:YES];

That's done! You will see Back Button of DetailViewController is "backTitle"

Linh Nguyen
  • 2,006
  • 1
  • 20
  • 16
0
- (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.

0

Your problem is probably very similar to the one described here: http://blog.tmro.net/2009/05/uitabbarbuttonitem-did-not-change-its.html

Try to debug to see if there are any _barButtonItemFlags set for your button...

nicktmro
  • 2,298
  • 2
  • 22
  • 31
  • It's nothing to do with that link! This problems involves resetting the title of the navigation BACK button programmatically, in a response to the user action of selecting a cell, and then selecting the the back button again. – PostCodeism Oct 19 '10 at 19:20
0

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.

Hamster
  • 111
  • 1
  • 4
-1

This is the best solution for this, don't set the navigationitem title as usual as self.navigationitem.title. Put this code on the parent view didload

    CGRect frame = CGRectMake(0, 0, 320, 44);
UILabel *label = [[[UILabel alloc] initWithFrame:frame] autorelease];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:20.0];
label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
label.textAlignment = UITextAlignmentCenter;
label.textColor = [UIColor whiteColor];

self.navigationItem.titleView = label;
label.text = @"My Navigation Bar";

By setting the title as above the actual navigation title will be nil, so the backbarbutton title will automatically be "Back"

-1
self.navigationController.navigationBar.backItem.title = @"TEXT";
Onnmir
  • 1,030
  • 14
  • 17
-1

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.

shanethehat
  • 15,460
  • 11
  • 57
  • 87
Segev
  • 1,267
  • 16
  • 21