tags:

views:

2291

answers:

8

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?

+10  A: 

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
But doesn't this create a button with the square style instead of the back-triangle style?
Marco
No, it makes a perfectly normal looking back button, like an arrow pointing to the left.
Chris Lundie
This doesn't work.
CodingWithoutComments
Yes it does. You should set it on the "parent" view controller, where the Back button will return to.
leolobato
Works perfect. Thanks.
Gerd
+1  A: 

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
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
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
A: 

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
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.
OOP_Master
+4  A: 

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
A: 

No need to mess with the backBarButtonItem.

In viewDidLoad replace:

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

With

self.navigationItem.title = @"Back";

And you're good to go.

thedude
Wrong! This set's the title of the controller
Gerd
A: 

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"

Ved Prakash
This is a very roundabout way to do it, and your title won't be centered anymore.
Hilton Campbell
A: 

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
If only if it was that simple....it doesn't work.
OOP_Master