0

So I want to change the text on the back button of a UIViewController, and I want it to satisfy the following criteria:

  1. Text can be changed dynamically, after the view is presented.
  2. Right swipe gesture from left edge of screen still works.

So far the answer I found is either How do I change the title of the "back" button on a Navigation Bar which is unable to change the text dynamically

or this https://stackoverflow.com/a/498089/2925345 which removes the swipe gesture.

Is there any way to satisfy both of the criterias?

Thanks

Community
  • 1
  • 1
Max One
  • 73
  • 1
  • 9
  • Why do you say http://stackoverflow.com/questions/1449339/how-do-i-change-the-title-of-the-back-button-on-a-navigation-bar is unable to change text dynamically? – heximal Mar 30 '15 at 09:53
  • no, once the view is presented, I tried to do `self.navigationItem.backBarButtonItem.text = @"smth else";` does not work, it works before the view is presented – Max One Mar 30 '15 at 10:12
  • what prevents you from recreation of the entire barbutton with different text again? – heximal Mar 30 '15 at 10:38
  • the swipe gesture doesn't work then – Max One Mar 30 '15 at 11:05
  • Screen Edge Swipe Gesture doesn't work when we present view over the other view it works only when a view push to other view. – Tanuj Mar 30 '15 at 11:17

3 Answers3

1

If not overridden, left button in navigation bar is constructed from navigationItem.backBarButtonItem of previous view controller. To change the text, you need to do something like

NSInteger count = self.navigationController.viewControllers.count;
if (count >= 2) {
    UIViewController* previousVC = self.navigationController.viewControllers[count-2];
    previousVC.navigationItem.backBarButtonItem.text = @"new text";
}
Jakub Vano
  • 3,833
  • 15
  • 29
0

Try this

 UIBarButtonItem *newBackButton =
[[UIBarButtonItem alloc] initWithTitle:@"NewTitle" style:UIBarButtonItemStylePlain
                                target:nil
                                action:nil];
[[self navigationItem] setBackBarButtonItem:newBackButton];
Binoy jose
  • 461
  • 4
  • 9
0

Finally solved this by assigning a delegate to UINavigation Controller's interactivePopGestureRecognizer. And it must be executed in or after viewWillAppear, in this particular order

self.navigationItem.backBarButtonItem = backButton;
self.navigationController.interactivePopGestureRecognizer.delegate = self;

Once I did that I can change the title of backBarButtonItem however I want.

Got my answer from

http://blog.csdn.net/zhaoxy_thu/article/details/15811189

I don't know how it works, I don't know why simply assigning a delegate to a swipe gesture recognizer enables the title of back button to be customizable, I simply followed the instructions in this magical blog post. Thanks zhaoxy_thu for making this work.

Max One
  • 73
  • 1
  • 9