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
  • 13,902
  • 33
  • 95
  • 134
  • 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
  • 39,286
  • 55
  • 198
  • 305
  • 8
    Unfortunately, will not work in a `prepareForSegue:sender:` callback. I am not sure what the accepted solution is for storyboards. – wcochran Jul 19 '12 at 18:35
  • 1
    I got this to work just fine in a prepareForSegue:sender: callback. Just get rid of [backButton release]; if you are using ARC. – dnorcott Sep 25 '12 at 17:19
  • 9
    One thing is worth emphasizing here: you set the backbutton on the current view controller, and you will see it in the next viewcontroller... in case it helps anyone.. it helped me. – Zsolt Jun 15 '13 at 08:33
  • Question: can't you just do `[[self navigationItem] setTitle:@"TEST"];`? This way you don't have to worry about setting up the target/action again (or the style). – Kezzer Jan 27 '14 at 07:53
  • 4
    @KieranSenior not working on iOS7 – UpCat Apr 01 '14 at 08:07
  • @artworkadシ Must be a specific case then, mine worked a treat. – Kezzer Apr 01 '14 at 09:04
  • Because Back button is related to the previous ViewController you need to apply these code in that controller in `viewDidLoad` method. – Alexander Volkov Sep 12 '14 at 18:45
  • 1
    Checked on iOS 8 and it didn't work. Changing the previous viewcontroller title was the only thing that did – José Manuel Sánchez Feb 17 '15 at 16:14
  • Use UIBarButtonItemStylePlain when minimum deployment target is iOS7 or later. UIBarButtonItemStyleBordered is deprecated. – Kiryl Bielašeŭski Mar 15 '16 at 12:28
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,035
  • 3
  • 55
  • 79
Jlam
  • 1,892
  • 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