Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 383
RexOnRoids Avatar asked Feb 04 '10 06:02

RexOnRoids


People also ask

How do I change the back button text in Swift?

In view controller A, create a UIBarButtonItem with any title you want and set it to navigationItem. backBarButtonItem . This will set a back button title to "You back button title here". To make the title empty, just set the title to nil or an empty string "" .

How do I show the back button in Swift?

Back-button text is taken from parent view-controller's navigation item title. So whatever you set on previous view-controller's navigation item title, will be shown on current view controller's back button text. You can just put "" as navigation item title in parent view-controller's viewWillAppear method.

How can remove back button in iOS?

Please navigate to Advanced Website Kiosk Settings–>Navigation–>Disable back button. Kindly enable this restriction to disallow the usage of the back button on the iOS device.


2 Answers

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

like image 72
Boon Avatar answered Oct 16 '22 21:10

Boon


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"

like image 12
Jlam Avatar answered Oct 16 '22 22:10

Jlam