el3ktro
el3ktro

Reputation: 167

Replace left navigation button with custom button, then back

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.

Upvotes: 4

Views: 1787

Answers (3)

user2816620
user2816620

Reputation: 1

See this other SO question.

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

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

Upvotes: 0

rmaddy
rmaddy

Reputation: 318794

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.

Upvotes: 3

Jacco
Jacco

Reputation: 3271

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.

Upvotes: 2

Related Questions