Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom text in back button of UINavigationController

In my app I have UINavigationController with two UIViewControler's (first and second). The first view controller contain UITableView. When a cell of table is touched the second view controller is pushed. What I want is to set custom text to back button at the second view. Here is the code in second view.

- (void)viewDidLoad
{
   [super viewDidLoad];
    self.navigationItem.leftBarButtonItem =
    [[[UIBarButtonItem alloc] initWithTitle:@"Go back"
                                      style:UIBarButtonItemStyleBordered
                                     target:nil
                                     action:nil] autorelease]; 
}

The problem is that the text of back button is changed but second view controller is not removed from the stack when the button is touched.

like image 342
OhDoh Avatar asked Jan 27 '26 18:01

OhDoh


2 Answers

This has been already asked so many times:

The back button (self.navigationItem.backBarButtonItem) must be set instead of left button and not in the second controller, but in the first one (root).

like image 74
Sulthan Avatar answered Jan 30 '26 10:01

Sulthan


The 'back' button applies to the controller doing the pushing. In this case, you would want to assign the custom back button in your 'first' UIViewController. When you push the second view controller, the back button will show the proper text

like image 35
Andrew Riebe Avatar answered Jan 30 '26 10:01

Andrew Riebe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!