Change item title with Segue button clicked

I am using push segue to show a new ViewController in my storyboard. By default, a back panel item on a floating navigation controller has the title of the "previous" (backing) controller.

To change the title of a back panel item to, for example, Back, I implemented the following in the ViewController that receives the click:

- (void)viewDidLoad {
    [super viewDidLoad];

    self.navigationItem.backBarButtonItem.title = @"Back";
}

      

But this does not change the title of the back panel item. I also tried using the newly created UIBarButtonItem with the same result: the backbar item is not being changed.

Another approach was to set the back panel element, for example prepareForSegue

in the controller where the segue is being executed.

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{

    if ([[segue identifier] isEqualToString:@"pushViewController"]) {
        UIViewController *viewController = [segue destinationViewController];
        viewController.navigationItem.backBarButtonItem.title = @"Back";
    }
}

      

With the same result: the title of the back panel item does not change. So how can I easily change the title of a backplane item? Thanks in advance!

+3


source to share


3 answers


To be more specific than Marco's answer, you need the following:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{        
    self.title = @"Back";
    // ...
}

      



Note that this is IMHO pretty annoying because you might end up having a whole state machine centered around what "self.title" should be at the current point in time, not just have every scene in the storyboard know what the back button has to say.

+3


source


Set the navigationItem.backBarButtonItem.title parameter in the source view controller, not the view controller.



+2


source


Add the title for the back panel item to the example "Storyboard-> VC-> NavigationItem" as "Back" and when preparing the segue set:

self.navigationItem.backBarButtonItem.title = @"Need a Title :)";

      

0


source







All Articles