5

I have a navigation view controller and there are 3 view controllers in the navigation stack. Now on the third and top most visible view controller I have a default back button coming in.

I need to bring this view controller in edit mode which I did... Now the requirement is to have a cancel button as the left bar button item instead of back button.

This is similar to the functionality given by contacts application of iPhone where you edit a particular contact.

Any clue how to achieve this?

Abhinav
  • 37,684
  • 43
  • 191
  • 309

3 Answers3

18

To hide back button and add a left bar button use-

[self.navigationItem setHidesBackButton:TRUE];

    UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector()];
    [self.navigationItem setLeftBarButtonItem:leftBarButton];
    [leftBarButton release];

And then to programmatically return to the previous view controller, you can do-

[self.navigationController popViewControllerAnimated:YES];
AbuZubair
  • 1,236
  • 14
  • 20
saadnib
  • 11,145
  • 2
  • 33
  • 54
0

This is a simpler way:

[self.navigationItem setHidesBackButton:YES];
rafaeljuzo
  • 379
  • 2
  • 8
-1

If you are using storyboard, you can also just drag a bar button item onto the navbar where the back button would normally show up. This will override it.

Chase Roberts
  • 9,082
  • 13
  • 73
  • 131