How to set the text of a back button on a UINavigationBar?
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"
Related videos on Youtube
Comments
-
RexOnRoids about 4 years
Possible Duplicate:
How do I change the title of the “back” button on a Navigation BarThe 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 callingself.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 about 14 yearsGood 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 about 12 yearsSuggest setting your "accept" to Boon's answer, which is correct. Matt's answer is not a good solution (sorry, Matt! :)
-
Peter Johnson almost 9 yearsChanging the back label is not against the HIG, it just has to make sense.
-
-
wcochran almost 12 yearsUnfortunately, will not work in a
prepareForSegue:sender:
callback. I am not sure what the accepted solution is for storyboards. -
dnorcott over 11 yearsI got this to work just fine in a prepareForSegue:sender: callback. Just get rid of [backButton release]; if you are using ARC.
-
Mark over 11 yearsIf 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 almost 11 yearsOne 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 about 10 yearsQuestion: 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 about 10 years@KieranSenior not working on iOS7
-
Dejan about 10 years@artworkadシ Must be a specific case then, mine worked a treat.
-
Alexander Volkov over 9 yearsBecause Back button is related to the previous ViewController you need to apply these code in that controller in
viewDidLoad
method. -
José Manuel Sánchez about 9 yearsChecked on iOS 8 and it didn't work. Changing the previous viewcontroller title was the only thing that did
-
Kiryl Bielašeŭski about 8 yearsUse UIBarButtonItemStylePlain when minimum deployment target is iOS7 or later. UIBarButtonItemStyleBordered is deprecated.