4

I have a UITableView within an UINavigationController. On the right of the UINavigationBar I have an "Edit" button. When I tap this button, all the textfields in the table cells become activated so I can edit them. The right button changes to "Save".

I'd now like the left "back" button of the UINavigationController to be replaced by a "Cancel" button which when I tap doesn't bring me back to the previous UIViewController, but just cancels editing mode.

When I tap "Save" the "Cancel" button should be changed back to the usual UINavigationController back button. Is there any easy way to do this? I tried accessing [[self navigationItem] leftBarButtonItem]. This works for the right button, but not the left one.

Jacco
  • 3,251
  • 1
  • 19
  • 29
el3ktro
  • 167
  • 2
  • 9
  • possible duplicate of [Custom back button for NavigationController for every screen](http://stackoverflow.com/questions/10516839/custom-back-button-for-navigationcontroller-for-every-screen) – Gabriele Petronella Jan 27 '13 at 18:51
  • 1
    That is not an appropriate duplicate. This question is not about creating a custom back button. This question is about temporarily replacing the back button with another button. – rmaddy Jan 27 '13 at 18:57

3 Answers3

3

Set the leftBarButtonItem to your Cancel button. At the appropriate time, set the leftBarButtonItem to nil to remove your Cancel button. This will automatically restore the original back button that was in place before you added the Cancel button.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
2

It sounds like the leftBarButtonItem is a "BackButton", which is set on the UIView(Controller) you used to navigate to the current View.

You can hide the BackButton following the answer in this question. This should be done, since you cannot change it's behavior.

I believe you are then able to use the leftBarButtonItem.

Community
  • 1
  • 1
Jacco
  • 3,251
  • 1
  • 19
  • 29
0

See this other SO question.

Then make sure you declare the cancel method similar to this.

(void)cancel:(id)sender {
   [self.navigationController popViewControllerAnimated:YES];
} 
Community
  • 1
  • 1