How to set the text of a back button on a UINavigationBar?

58,971

Solution 1

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

Solution 2

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"

Share:
58,971

Related videos on Youtube

RexOnRoids
Author by

RexOnRoids

Just some kid.

Updated on April 23, 2020

Comments

  • RexOnRoids
    RexOnRoids about 4 years

    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)?

    • Justin Searls
      Justin Searls about 14 years
      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.
    • Olie
      Olie about 12 years
      Suggest setting your "accept" to Boon's answer, which is correct. Matt's answer is not a good solution (sorry, Matt! :)
    • Peter Johnson
      Peter Johnson almost 9 years
      Changing the back label is not against the HIG, it just has to make sense.
  • wcochran
    wcochran almost 12 years
    Unfortunately, will not work in a prepareForSegue:sender: callback. I am not sure what the accepted solution is for storyboards.
  • dnorcott
    dnorcott over 11 years
    I got this to work just fine in a prepareForSegue:sender: callback. Just get rid of [backButton release]; if you are using ARC.
  • Mark
    Mark over 11 years
    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.
  • Zsolt
    Zsolt almost 11 years
    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.
  • Dejan
    Dejan about 10 years
    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).
  • DarkLeafyGreen
    DarkLeafyGreen about 10 years
    @KieranSenior not working on iOS7
  • Dejan
    Dejan about 10 years
    @artworkadシ Must be a specific case then, mine worked a treat.
  • Alexander Volkov
    Alexander Volkov over 9 years
    Because Back button is related to the previous ViewController you need to apply these code in that controller in viewDidLoad method.
  • José Manuel Sánchez
    José Manuel Sánchez about 9 years
    Checked on iOS 8 and it didn't work. Changing the previous viewcontroller title was the only thing that did
  • Kiryl Bielašeŭski
    Kiryl Bielašeŭski about 8 years
    Use UIBarButtonItemStylePlain when minimum deployment target is iOS7 or later. UIBarButtonItemStyleBordered is deprecated.