0

I have a push segue from a button to a new view and would like to change the backbutton title. Can you help me whats wrong with this code?

Here is a code snippet:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"settings"]) {
        NSLog(@"prepareForSegue: %@%@", segue.identifier, segue.destinationViewController);

        UIViewController *viewController = [segue destinationViewController];
        viewController.navigationItem.backBarButtonItem.title = @"Back";

        [segue.destinationViewController setTitle:@"WeekView"];
    }
}

The setTitle does work. Thanks in advance!!

rmaddy
  • 314,917
  • 42
  • 532
  • 579
mtStack
  • 65
  • 1
  • 7

3 Answers3

0

The UIViewController's view hasn't loaded yet, so you first set the title and then the view loading code happens and it sets the title to the default one. Try setting the back button title in the viewDidLoad or viewWillAppear methods of the destination view controller.

The view controller's title property is set and works, because the segue has already instantiated the view controller itself.

Andrew
  • 3,166
  • 21
  • 32
0

self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:nil action:nil]]; normally it should be done in viewDidLoad, changing the navigation button title when segue to another view controller is not a good idea.

Suhit Patil
  • 11,748
  • 3
  • 50
  • 60
0

self.navigationItem.backBarButtonItem defaults to nil. So if you set the title to it, nothing will happen because it is affecting a nil address object. This won't throw an error, because objective-c allows messaging nil objects.

ANSWER: You must create your own button object in order to set it's title. You can initWithTitle or create it and then set the title afterward.(Explianed HEre)

For Your reference :

newBackButton = [[UIBarButtonItem alloc] initWithTitle: @"Home" style: UIBarButtonItemStyleBordered target: self action: @selector(Home:)];
    self.navigationItem.leftBarButtonItem=newBackButton;
Community
  • 1
  • 1
Kumar KL
  • 15,315
  • 9
  • 38
  • 60
  • I've also tried to change the text within the .m file of the new view. But this doesn't work too. Do you know why? - (IBAction)test:(id)sender { self.navigationItem.backBarButtonItem.title=@"Back"; } – mtStack Nov 13 '13 at 10:47
  • Please read the answer fully. You need to create the custom UIBarbutton . It may due to to some childs controller and UInavigation Stack – Kumar KL Nov 13 '13 at 11:58