I am using storyboards in my application, and if I set the title property of a ViewController class, that string will appear as the text of my back button when I push a SecondViewController, how can I change this?, I want to put a different title for the back button.
I have used this but it doesn't work:
UIBarButtonItem *btnBack = [[UIBarButtonItem alloc]
initWithTitle:@"Back"
style:UIBarButtonItemStyleBordered
target:self
action:nil];
[self.navigationItem setBackBarButtonItem: btnBack];
please help me
Back-button text is taken from parent view-controller's navigation item title. So whatever you set on previous view-controller's navigation item title, will be shown on current view controller's back button text. You can just put "" as navigation item title in parent view-controller's viewWillAppear method.
Try this ,
Objective-C:
UIBarButtonItem *btnBack = [[UIBarButtonItem alloc]
initWithTitle:@"Done"
style:UIBarButtonItemStyleBordered
target:self
action:nil];
self.navigationController.navigationBar.topItem.backBarButtonItem=btnBack;
Swift:
var btn = UIBarButtonItem(title: "Done", style: .Plain, target: self, action: "backBtnClicked")
self.navigationController?.navigationBar.topItem?.backBarButtonItem=btn
When you use Storyboards you can change the text of the left bar button item aka the back button with no code. You will obviously need a UINavigationController
within your Storyboard so you have the ability to push and pop views.
Then when a view is pushed from this view it will use this title set.
Set a backBarButtonItem to the navigationItem of the previous viewController. Setting the topItem only works on the first time viewWillAppear. Please check this post for detail: iOS Set Navigation Bar Back Button Title
NSArray *viewControllerArray = [self.navigationController viewControllers];
// get index of the previous ViewContoller
long previousIndex = [viewControllerArray indexOfObject:self] - 1;
if (previousIndex >= 0) {
UIViewController *previous = [viewControllerArray objectAtIndex:previousIndex];
previous.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]
initWithTitle:backButtonTitle
style:UIBarButtonItemStylePlain
target:self
action:nil];
}
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