I'm trying to set the BACK button for a pushed VC set within a UINavigationController stack. I use the following code, and it's not working - I still get the previous VC name appearing as the back button title.
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.title = @"VC Title";
UIBarButtonItem* myBackButton = [[UIBarButtonItem alloc]
initWithTitle:@"Back"
style:UIBarButtonItemStyleBordered
target:nil
action:nil];
self.navigationItem.backBarButtonItem = myBackButton;
}
Anyone?
You can do: [self. navigationItem setHidesBackButton:YES]; In your second view controller (the one you want to hide the button in).
The back button sort of belongs to the parent view controller. The Navigation Item gives you a handle to the back button, so you can set the title in code or in the Storyboard. If you leave the Navigation Item Back Button text as the default empty string, the back button title will become "Back". Other approaches work, why use this one?:
By default the view controller title is synced with the navigation item title. @Rincewind you said: "The navigation menu titles come from the navigation item's back button title first on the assumption that clients who choose to override that title do so because it is a better title than the navigation item's title."
Let's supouse that you have a navigation controller going to ViewA from ViewB. In IB, select ViewA's navigation bar, you should see these options: Title, Prompt and Back Button. ViewA navigate bar options The trick is choose your destiny view controller back button title (ViewB) in the options of previous view controller (View A).
While it's possible to override the back button title on the child view controller, it was a challenge getting a handle to it until it had already flashed briefly on the screen. Some of the approaches construct a new back button and override the existing one. I'm sure it works, and probably necessary in some use cases.
in parent view controller:
- (void)viewWillDisappear:(BOOL)animated
{
self.title = @"Back";
}
- (void)viewWillAppear:(BOOL)animated
{
self.title = @"Title of your navigation bar";
}
Will do the trick
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With