How to change uinavigationbar back button text

21,676

Solution 1

You need to change self.navigationItem.backBarButtonItem from previous view, not current view (I know, it seems to be a little bit illogical). For example, in your table view you can do the following:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setTitle:@"My title"];
    UIBarButtonItem *boton = [[UIBarButtonItem alloc] initWithTitle:@"Custom back button text" style:UIBarButtonItemStyleBordered target:self action:@selector(mySelector:)];
    self.navigationItem.backBarButtonItem  = boton;
    [boton release];
}

Solution 2

This is where the documentation is not so clear until you have read and re-read and play around with each settings.
To change the title of the default back button add this line in viewDidLoad()

self.navigationController.navigationBar.topItem.title = @"Your Label";

If you want the button to be invisible - then set the value to @"" empty string.

Solution 3

Okay figured it out, I posted this code in the parent views tableView:didSelectRowAtIndexPath: method. this is how my one looks with multiple tablecells the user can select. Hope this helps.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.
    if (indexPath.section == 0) { //--- picks (first) section

     ChildViewController *childViewController = [[ChildViewController alloc] initWithNibName:@"ChildViewController" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
     [self.navigationController pushViewController:childViewController animated:YES];
    //--- this sets the back button to "Back" every time you load the child view.
     self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Back" style: UIBarButtonItemStyleBordered target:nil action:nil] autorelease];

        if(indexPath.row == 0) { //--- picks row
            childViewController.title = @"Bike";
        }
        if(indexPath.row == 1) {
            childViewController.title = @"Model";
        }
        [vehicleSearchResponseTableViewController release];
    }


}

Solution 4

The best way with Xcode 5 to change the back button name is to edit the Back Button field in IB of the Navigation Item on the View Controller to which the back button will return. For example, if you have a list TableViewController that goes to a detail screen, change the Back Button on the list TableViewController's Navigation item (in IB) to change the back button name that the detail screen displays.

Solution 5

It's not gonna work the way you're trying to do. Navigation button will always have title of previous view. What you can do though - change title of first view before pushing the new one. This is the only was I could find to solve same problem.

Share:
21,676
C.Johns
Author by

C.Johns

Updated on July 05, 2022

Comments

  • C.Johns
    C.Johns almost 2 years

    I am currently trying to change the back button text of a subview that is loaded of a tablecell touch. However even with the way I am trying to implement it it still shows the parents title in the back button.

    I am trying to load a new value into the back button inside viewdidload method like so

    UIBarButtonItem *myBarButtonItem = [[UIBarButtonItem alloc] init];
    myBarButtonItem.title = @"Back";
    self.navigationItem.backBarButtonItem = myBarButtonItem;
    [myBarButtonItem release];
    

    however its not working.

  • C.Johns
    C.Johns over 12 years
    and then when you go back your have to change your title back.. this is a horrible solution if its the only possibility.. fingers crossed there is another way.
  • Jake Long
    Jake Long almost 11 years
    this is such a hack but it works if you reset the previous nib's title again when you go back to it
  • Oded Ben Dov
    Oded Ben Dov over 10 years
    According to your explanation, don't you mean [self.navigationItem setTitle:@"..."] or even just 'self.title = @"..."'?
  • Peter
    Peter almost 10 years
    Be default self.navigationItem.backBarButtonItem is nil so you cannot change the back button rite using this.
  • JVillella
    JVillella about 9 years
    This is the way you want to do it. Thanks Peter.
  • Fábio Oliveira
    Fábio Oliveira about 9 years
    Downvoted. Considering the other options this shouldn't be considered a viable solution. If we were facing by a lack of control on how the back button behaves this would then be an acceptable hack, but an hack still.
  • Pat Long - Munkii Yebee
    Pat Long - Munkii Yebee almost 9 years
    The TopItem is not always a BackButton in one case for me the TopItem was the View title
  • Steee
    Steee over 8 years
    Not if he was writing it in spanish ;)
  • neowinston
    neowinston about 8 years
    If you have more than one view controller on the stack this is the only solution that really works. Thanks.