19

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
  • 10,185
  • 20
  • 102
  • 156
  • Is this being performed on the main thread? – Jordaan Mylonas Aug 30 '11 at 01:44
  • To be honest I am not sure, I am not sure about threads in the ios.. I am not creating threads to my knowledge if that helps answer you question. – C.Johns Aug 30 '11 at 01:55
  • 1
    Duplicate Question. See this answer: http://stackoverflow.com/a/4291832/1627732 – Yoga Oct 29 '13 at 18:58
  • possible duplicate of [How do I change the title of the "back" button on a Navigation Bar](http://stackoverflow.com/questions/1449339/how-do-i-change-the-title-of-the-back-button-on-a-navigation-bar) – Oded Ben Dov Jan 15 '14 at 07:47

10 Answers10

30

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];
}
Alfred
  • 21,058
  • 61
  • 167
  • 249
Juanjo
  • 401
  • 4
  • 3
26

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.

Spidy
  • 1,137
  • 3
  • 28
  • 48
Hung Tran
  • 440
  • 4
  • 6
  • 2
    this is such a hack but it works if you reset the previous nib's title again when you go back to it – Jake Long Jun 03 '13 at 14:52
  • 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. – Fábio Oliveira Mar 26 '15 at 14:16
  • The TopItem is not always a BackButton in one case for me the TopItem was the View title – Pat Long - Munkii Yebee May 28 '15 at 14:54
9

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


}
C.Johns
  • 10,185
  • 20
  • 102
  • 156
  • If you have more than one view controller on the stack this is the only solution that really works. Thanks. – neowinston Mar 19 '16 at 10:41
2

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.

Peter
  • 288
  • 3
  • 8
1

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.

sha
  • 17,824
  • 5
  • 63
  • 98
  • 1
    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. – C.Johns Aug 30 '11 at 02:30
0

After viewDidLoad

self.navigationController!.navigationBar.backItem?.title = "Back"

mashern
  • 337
  • 4
  • 10
0

My suggestion was to add a separate Label to parents Page title Bar. Worked fine for me.

let titleLabel = UILabel(frame: CGRect(x: (view.frame.width * (3/8)), y: 0, width: (view.frame.width * 0.25 ), height:30))
        titleLabel.text = "Title"
        titleLabel.textAlignment = .center
        titleLabel.textColor = UIColor.white
        titleLabel.font = UIFont.systemFont(ofSize: 20)
        navigationItem.titleView = titleLabel 
gamal
  • 1,587
  • 2
  • 21
  • 43
0

Your code looks fine so far.

Is this code executed before the

[super viewDidLoad];

statement (wrong) or after it (good)?

Hermann Klecker
  • 14,039
  • 5
  • 48
  • 71
-1

This article should do what you want.

From the author:

As you can see above, all you need to do is set the leftBarButtonItem of the controller and it will hide the back button. The selector handleBack now handles the back event and you need to then manually pop the view controller off the UINavigationController’s stack. You can implement any of your own code in there, even blocking leaving the page.

Ali
  • 2,439
  • 23
  • 13
-3

A good way to do this is to set the title of the back button in the parent view controller before calling the child like this:

[self.navigationItem.backBarButtonItem setTitle:@"Your Custom Title"];

This title will be placed in the back button on the child view. The important point to get here is this title is used in the child view as the back button to the parent view.

Abdurrahman Mubeen Ali
  • 1,331
  • 1
  • 13
  • 19
T.J.
  • 3,942
  • 2
  • 32
  • 40
  • According to your explanation, don't you mean [self.navigationItem setTitle:@"..."] or even just 'self.title = @"..."'? – Oded Ben Dov Jan 15 '14 at 07:45
  • 1
    Be default self.navigationItem.backBarButtonItem is nil so you cannot change the back button rite using this. – Peter Jul 15 '14 at 15:12