Replace the left navigation button with a custom button, then back

I have a UITableView inside a UINavigationController . To the right of the UINavigationBar , I have a Edit button. When I click this button, all the text fields in the table cells are activated, so I can edit them. The right button will change to "Save."

I would like the left back button of the UINavigationController be replaced by the Cancel button, which, when I click, does not return me to the previous UIViewController , but simply cancels the editing mode.

When I click "Save", the "Cancel" button should be replaced with the regular UINavigationController button back. Is there an easy way to do this? I tried to access [[self navigationItem] leftBarButtonItem] . This works for the right button, but not for the left.

+4
source share
3 answers

Set leftBarButtonItem to the Cancel button. Set the leftBarButtonItem to nil at the appropriate time to remove the Cancel button. This will automatically restore the original return button that was in place before you added the Cancel button.

+3
source

It appears that leftBarButtonItem is the โ€œBackButtonโ€ that is set to the UIView (Controller) that you used to jump to the current view.

You can hide BackButton after the answer in this question . This must be done since you cannot change its behavior.

I believe that then you can use the leftBarButtonItem element .

+2
source

See this other SO question .

Then make sure you declare a cancellation method similar to this.

 (void)cancel:(id)sender { [self.navigationController popViewControllerAnimated:YES]; } 
0
source

All Articles