Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing the UIBackButtonItem title

Tags:

cocoa-touch

I have a navigationController-based app. I want to change the title of the back button for the root view controller. I have tried the following code in the rootViewController's viewDidLoad method, but no success:

self.navigationItem.backBarButtonItem.title = @"Back"; 

Any ideas?

like image 618
Alpinista Avatar asked Jan 31 '09 01:01

Alpinista


People also ask

How do I change the title of UIBarButtonItem in Swift?

In case you want to initialize UIBarButtonItem programmatically in Swift 4+ should be done as following: editButton = UIBarButtonItem(title: "Edit", style: . plain, target: self, action: #selector(editAct)).

How do I change my UIBarButtonItem text?

Select the UIBarButtonItem in Interface Builder , open the inspector (Command + Shift + I) , and select "Custom" under the dropdown next to Identifier .


2 Answers

I've had success by creating my own UIBarButtonItem instead of setting the title of the existing one:

UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:nil action:nil]; self.navigationItem.backBarButtonItem = backButton; [backButton release]; 
like image 86
Chris Lundie Avatar answered Sep 27 '22 17:09

Chris Lundie


The title of the back button is either:

  1. The title of the previous view controller
  2. The name of the previous view controller's navigation item back button

If you are setting the back button for the current view controller's navigation item you are not setting the button that get's displayed in the current view. You are in fact setting the back button that will be used if you push another view controller from it.

like image 25
Mihai Damian Avatar answered Sep 27 '22 17:09

Mihai Damian