birdcage
birdcage

Reputation: 2676

Navigation bar back button text is not changing

I am trying to change the text of the navigation bar back button text. I have tried the solutions offered here but I cant get the text I want on back button. In appdelegate I set these features:

 [[UINavigationBar appearance] setBarTintColor:[self colorWithHexString:@"e74c3c"]];
 [[UINavigationBar appearance] setBarTintColor:[self colorWithHexString:@"e74c3c"]];   

[[UINavigationBar appearance] setTitleTextAttributes:
 [NSDictionary dictionaryWithObjectsAndKeys:
  [UIColor whiteColor], UITextAttributeTextColor,
  [UIFont fontWithName:@"Futura-CondensedMedium" size:19.0], UITextAttributeFont,nil]];

NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowOffset = CGSizeMake(0.0, 1.0);
shadow.shadowColor = [UIColor whiteColor];

[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil]
 setTitleTextAttributes:
 @{NSForegroundColorAttributeName:[UIColor whiteColor],
   NSShadowAttributeName:shadow,
   NSFontAttributeName:[UIFont fontWithName:@"Futura-CondensedMedium" size:19.0]
   }
 forState:UIControlStateNormal];

[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];

And in every viewcontroller's viewdidload I set:

self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"SomeText" style:UIBarButtonItemStylePlain target:nil action:nil];

or

UIBarButtonItem *newBackButton = 
        [[UIBarButtonItem alloc] initWithTitle:@"NewTitle" 
                                         style:UIBarButtonItemStyleBordered 
                                        target:nil 
                                        action:nil];
[[self navigationItem] setBackBarButtonItem:newBackButton];

Any suggestions?

Upvotes: 2

Views: 3575

Answers (3)

Weidian Huang
Weidian Huang

Reputation: 2945

The back button belongs to the previous view controller, not the one currently presented on screen. To modify the back button you should update it before pushing, on the view controller that initiated the segue:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Delete back item title in next controller
    UIBarButtonItem *newBackButton =
    [[UIBarButtonItem alloc] initWithTitle:@"New Title"
                                     style:UIBarButtonItemStylePlain
                                    target:nil
                                    action:nil];
    [[self navigationItem] setBackBarButtonItem:newBackButton];
}

Upvotes: 1

mhrrt
mhrrt

Reputation: 977

If I haven't misunderstood you you can do this to set the title of the back button. Suppose there are three view controllers: A, B and C. Now you want the title of the back button to be "MyViewA" when you navigate from A to B then you need to put this code in "viewWillAppear:" of your ViewController A (this will show the title of the back button as "MyViewA" when you are on view controller B).

 //viewWillAppear of ViewController A
 -(void)viewWillAppear:(BOOL)animated{

       //now set title of back button
       self.navigationItem.backBarButtonItem =
       [[UIBarButtonItem alloc] initWithTitle:@"YourTitle"
                                  style:UIBarButtonItemStyleBordered
                                 target:nil
                                 action:nil];
 }

Likewise if you are navigating from B to C and want to set the back button title for ViewController C, you need to put the above code in "viewWillAppear:" delegate of ViewController B. In short you need to set the title of the next view controller on your current view controller.

Upvotes: 5

Heckscheibe
Heckscheibe

Reputation: 640

You can't change the value of the text in the backbutton. You have to set the button as the left or right barButtonItem (probably left if you want it as back button) like this:

[self.navigationItem setLeftBarButtonItem:newBackButton];

you can then attach an IBAction to the button by setting it when you init your button:

UIBarButtonItem *newBackButton = 
    [[UIBarButtonItem alloc] initWithTitle:@"NewTitle" 
                                     style:UIBarButtonItemStyleBordered 
                                    target:nil 
                                    action:yourBackFunction];

at least that would be a workaround and I have also used it before...

Upvotes: 1

Related Questions