tags:

views:

1655

answers:

4

The "back button" by default shows the title of the last view in the stack, is there a way to have custom text in the back button instead?

+5  A: 

From this link:

self.navigationItem.backBarButtonItem =
  [[UIBarButtonItem alloc] initWithTitle:@"Custom Title"
            style:UIBarButtonItemStyleBordered
           target:nil
           action:nil];
rein
You should `release` the `UIBarButtonItem`.
Mehrdad Afshari
Also note, you need to do this in the view controller one level up the stack. In other words, don't do this in the visible view controller, but in the view controller that you'd see if you hit the back button.
Tyler
A: 

I found a handy solution to this by simply setting the title of the controller before pushing another controller onto the stack, like this:

self.navigationItem.title = @"Replacement Title";
[self.navigationController pushViewController:newCtrl animated:YES];

Then, make sure to set the original title in viewWillAppear, like this:

-(void)viewWillAppear:(BOOL)animated
{
  ...
  self.navigationItem.title = @"Original Title";
  ...
}

This works because the default behavior of UINavigationController when constructing the back button during a push operation is to use the title from the previous controller.

Aubrey Goodman
The problem with this is the title changes on the old view as it animates out and looks a bit naff. None of the given solutions here seem to work as you'd like :-(
Danny Tuppeny
A: 

Expanding on Aubrey's suggestion, you can do this in the child view controller:

create two variables for storing the old values of the parent's navigationItem.title and the parent's navigationItem

UINavigationItem* oldItem;
NSString* oldTitle;

in viewDidLoad, add the following:

oldItem = self.navigationController.navigationBar.topItem;  
oldTitle = oldItem.title;  
[oldItem setTitle: @"Back"];  

in viewWillDisappear, add the following:

[oldItem setTitle: oldTitle];  
oldTitle = nil;  // do this if you have retained oldTitle
oldItem = nil;   // do this if you have retained oldItem

It's not perfect. You will see the the title of the parent view change as the new controller is animated in. BUT this does achieve the goal of custom labeling the back button and keeping it shaped like a standard back button.

FingerTipFun
A: 

I use this:

// In the current view controller, not the one that is one level up in the stack
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.backItem.title = @"Custom text";
}
Trang
This almost works for me, but when I click the back button, the text changes back to the title of the parent for the duration of the animation :-(
Danny Tuppeny
This changes the Back button title of the controller up in the stack. It does not change the Back button title of the currently displayed controller. Even worse: it changes the title of the controller previous to the previous controller!
Colins