Erik Villegas
Erik Villegas

Reputation: 984

Change back button title after push

Is it possible to change the title of the back button after the UINavigationController completes the push?

For example, say I have a text field on a pushed VC. Is it possible to mirror the field text with the back button text?

I know this is bad but a client requires it.

Upvotes: 1

Views: 2024

Answers (3)

quaertym
quaertym

Reputation: 3961

Use this:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    self.navigationController.navigationBar.backItem.title = self.textField.text;
}

However you may need to set backItem to an UIBarButtonItem instance before setting the title since it could be nil.

Upvotes: 0

Chris
Chris

Reputation: 1673

The back button's title is derived from the previous view controller's title. So if you want to mirror the text field the back button title, you should pass the view controller's title to the pushed view controller in your prepare for segue method:

/*
* Called in ViewControllerA
*/
if ([segue.identifier isEqualToString:@"ViewControllerB"]) {
    ViewControllerB *VCB = segue.destinationViewController;
    VCB.previousViewControllerTitleString = self.navigationItem.title;
}

Then you can use the previousViewControllerTitleString property in ViewControllerB to set the textfield's text.

Upvotes: 2

scorpiozj
scorpiozj

Reputation: 2687

1 Supposing 2 viewController, A pushed to B:

when you push B in A, you can set A's title what you want. And then the back title in B will be changed.

Please Notice to change back the title of A (if you need) when B is popped.

2 you can also custom the back button yourself.

Upvotes: 1

Related Questions