Reputation: 3741
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?
Upvotes: 30
Views: 23948
Reputation: 1
- (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.
Upvotes: 0
Reputation: 2036
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"
Upvotes: 0
Reputation: 83
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];
}
Upvotes: 0
Reputation: 4881
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;
Upvotes: 1
Reputation: 111
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.
Upvotes: 0
Reputation: 1297
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.
Upvotes: -1
Reputation: 11432
The title of the back button is either:
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.
Upvotes: 50
Reputation: 63616
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"
;
Upvotes: 5
Reputation: 11
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"
Upvotes: -1
Reputation: 7918
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
.
Upvotes: 26
Reputation: 2288
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...
Upvotes: 0
Reputation: 43462
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).
Upvotes: 1
Reputation: 6023
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];
Upvotes: 52