简体   繁体   中英

Disabling back button and put Cancel button in navigation view controller — like contacts iPhone application

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?

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

这是一种更简单的方法:

[self.navigationItem setHidesBackButton:YES];

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM