0

My view controller have a back button show on navigation bar(in a UINavigationController). There is a "edit" mode in current view. When into "edit" mode, I replace the leftBarButtonItem to a "Cancel" button, when exit the "edit" mode, I want to change the left button to "back button" again, how can I do it?

I try self.navigationItem.leftBarButtonItem = self.navigationItem.backBarButtonItem, it just remove the "Cancel" button, but not get the "back button" back.

Now I can create a new navigation item with the UI just like the back button, and set it to as leftBarButtonItem, but the position is not same with the native back bar button, and lost the swipe back gesture.

Is there any way to get the native back bar button item back on the navigation bar?

zgjie
  • 2,099
  • 2
  • 21
  • 32
  • Back button is default button when you use push navigation. You need to comment code related to `leftBarButtonItem`. Also remove above line you specified in question. Are you trying to create custom button which looks like default one? – Kampai Nov 07 '14 at 09:39
  • @Kampai I want the default one. – zgjie Nov 07 '14 at 09:44

2 Answers2

5

When you leave edit mode, just set:

self.navigationItem.leftBarButtonItem = nil;
pbasdf
  • 21,386
  • 4
  • 43
  • 75
0
  • In same view controller just add self.navigationItem.backBarButtonItem.title = @"Cancel"; in viewWillAppear.
  • And add self.navigationItem.backBarButtonItem.title = @"Back"; in your viewDidDisappear to set back button text of your class file .m.

To set Cancel :

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationItem.backBarButtonItem.title = @"Cancel";
}

To set Back :

-(void)viewDidDisappear:(BOOL)animated
{
    self.navigationItem.backBarButtonItem.title = @"Back";
}
Rajesh Loganathan
  • 11,129
  • 4
  • 78
  • 90
  • I have not move to a new view, just add a "edit" state in current view, when in "edit" state, the left button is "Cancel", but when exit "edit" state, I want get the default "back" bar button back. Thanks. – zgjie Nov 07 '14 at 09:47
  • No issues. Just change the title text for self.navigationItem.backBarButtonItem.title = @"" in your edit and exit state. – Rajesh Loganathan Nov 07 '14 at 09:59