43

Possible Duplicate:
How do I change the title of the “back” button on a Navigation Bar

The Situation:
I have a UIViewController that is governed by a navigation controller. I am able to set the title of the navigation bar covering the UIViewController by simply calling self.title = @"title". But, I want to be able to set the back button text of the navigation bar, too (for different languages n' such..)

The Problem:
I don't know how to set the back button's text.

The Question:
How do I set the back button of the UINavigation bar (programmatically not in IB)?

Community
  • 1
  • 1
RexOnRoids
  • 14,002
  • 33
  • 96
  • 136
  • Good question. There may not be a documented way to do this (I honestly don't know), but I'd be surprised if changing it to anything that isn't the title of the previous item or the word "Back" wasn't against the HIG. And if it is against the HIG, there's a good chance this customization could lead to an app rejection. /2cents. – Justin Searls Feb 04 '10 at 06:19
  • 3
    Suggest setting your "accept" to Boon's answer, which is correct. Matt's answer is not a good solution (sorry, Matt! :) – Olie Feb 20 '12 at 20:47
  • Changing the back label is not against the HIG, it just has to make sense. – Peter Johnson Jun 12 '15 at 13:41

2 Answers2

148

The setTitle way - though may seem to work, is incorrect. If you pay attention closely, you will notice that the navigation item changes to the new title while the new view is animated into view via pushViewController. The fact that you have to restore your title in viewWillAppear is kludgy, which further suggests its hack nature.

The correct way is to do the following before your pushViewController:

UIBarButtonItem *backButton = [[UIBarButtonItem alloc] 
                                initWithTitle: @"Back Button Text" 
                                style: UIBarButtonItemStyleBordered
                                target: nil action: nil];

[self.navigationItem setBackBarButtonItem: backButton];
[backButton release];

Boon
  • 40,656
  • 60
  • 209
  • 315
12

The best and easiest way according to apple documentation to set the view controller's title to what the back should say is this -

Example:

If viewController1 is under viewController2 (and the back button on viewController2 is going to viewController1)

viewController1.title = @"something awesome";

the back button on viewController2 will show "something awesome"

TheTiger
  • 13,264
  • 3
  • 57
  • 82
Jlam
  • 1,932
  • 1
  • 21
  • 26
  • 2
    If you add a title to the view controllers in the navigation stack, that title will appear as the back button's title. "Back" is the default text. You don't need to remove this title after it's been set either. Therefore this is the correct answer. – Mark Jan 09 '13 at 19:17