27

Trying to customize my back button in a drilldown navigation controller.

On my one view controller I have an Add button where the code programatically generates a new UIViewController:

- (void)add:(id)sender 
{
    MyAddViewController *addController = [[MyAddViewController alloc] initWithNibName:@"MyAddViewController" bundle:nil];

    [self.navigationController pushViewController:addController animated:YES];

    [addController release];
}

This works and when I click the add button it drills down into the new view. Inside the viewDidLoad method of MyAddViewController.m I have:

self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:nil action:nil] autorelease];

But this isn't working. The back button in the navigation controller remains the title of the previous view's controller on the stack. It seems that line does nothing. Did I miss something?

Thanks

shim
  • 9,289
  • 12
  • 69
  • 108
skålfyfan
  • 4,931
  • 5
  • 41
  • 59

5 Answers5

59

self.navigationItem.backBarButtonItem is for the back button that appears on the view pushed by the view controller. So you need to move that line to the previous view controller.

benwong
  • 2,226
  • 1
  • 16
  • 14
31

This will only work on each child after the viewController that has self.navigationItem.backBarButtonItem.

WrightsCS
  • 50,551
  • 22
  • 134
  • 186
14

You're confusing the backBarButtonItem and the leftBarButtonItem. From the UINavigationItem docs on backBarButtonItem:

When this item is the back item of the navigation bar—when it is the next item below the top item—it may be represented as a back button on the navigation bar. Use this property to specify the back button. The target and action of the back bar button item you set should be nil. The default value is a bar button item displaying the navigation item’s title.

So, if you were to change:

self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:nil action:nil] autorelease];

To:

self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:nil action:nil] autorelease];

I believe you would get the desired effect.

shim
  • 9,289
  • 12
  • 69
  • 108
Matt Wilding
  • 20,115
  • 3
  • 67
  • 95
  • 19
    ...except for not being a 'proper back button', that is, it will be missing the arrow shape. – Julian D. Jan 23 '12 at 16:50
  • tyvm +1 - I just lost an hour trying to assign to backBarButtonItem when leftBarButtonItem was exactly what I needed! – kfmfe04 Dec 27 '14 at 05:33
5

You can't replace the backBarButtonItem, but you can use the leftBarButtonItem to override it. But to get the new button to perform operate the same as the back button, you do need to set the target and action of the new button something like:

- (void)dismissMyView {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]
        initWithTitle:@"Quit" style: UIBarButtonItemStyleBordered
        target:self action:@selector(dismissMyView)];
}
Himanshu
  • 31,810
  • 31
  • 111
  • 133
Greg Young
  • 543
  • 1
  • 6
  • 11
1

If ViewController A push ViewController B meanwhile we want to set the back bar button tittle, we should set "self.navigationItem.backBarButtonItem = ..".if it was set in ViewController B, it will not work as we want.

lynulzy
  • 561
  • 7
  • 19