I have a UIViewController
and I'm navigating from my first view controller to second view controller and I want to change the name of the button shown on the navigationcontroller
for going back ....
SecondViewController *secondController = [[SecondViewController alloc]
initWithNibName:nil
bundle:NULL];
[self.navigationController pushViewController:secondController animated:YES];
now in the second viewcontroller
I want change the name of the button in the navigationController
.
There is an easy way of doing that in Interface Builder or Storyboarding. In the parent View Controller, just set the attribute "Back Button" of the navigation item to whatever title you want. Here is an example:
Villian's Tavern View will have "Back" as Back Button Title, like we just set in the attributes inspector of the parent's navigation controller (Best Bars).
Swift 3:
Note: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:
If you wish to do this programmatically, it can be done like this:
Objective-C
Swift
However, if you prefer using Interface Builder, just select the UINavigationItem that you wish to set the back button for, and navigate to the attributes inspector to change the back button's title.
NOTE: It is important to remember that you must configure this on the view controller that you would be returning to upon tapping the back button, not the currently visible view controller.
There are two ways to do this.
First way is by program code:
Yakimenko Aleksey's answer is the simplest way, This really works ( tested on Xcode 8 + iOS 10)
Swift3:
Note: you should call above code in source view controller, not the destination view controller.
Second way is use storyboard localization via
Main.strings
file,The nnnnnnn means the object id of the BackBarButtonItem in the source view controller(not the destination view controller!)
You need set the "back" property of the navigation bar item, it will auto create a BarButtonItem, you find its object id.
Sorry, i failed to upload screenshots.
If you use Storyboard references, the View Controller might not display the Navigation Item on Interface Builder.
Manually dragging a Navigation Bar will not work, you need to drag a Navigation Item
You'll then be able to set its back button's title. This is also where you'll set the view's title.
Quoting Mick MacCallum:
NOTE: It is important to remember that you must configure this on the view controller that you would be returning to upon tapping the back button, not the currently visible view controller.
I had a rather complex storyboard with many navigation controllers, and my customer wanted all of the back buttons to say "Back"
I went through in Interface Builder and put "Back" into the Back button field of every UINavigationItem.
I was not working for every scene change!
The problem was that not every view controller in the storyboard had a UINavigationItem.
I fixed this by dragging out a new UINavigationItem onto every UIViewController that did not have one, and setting all of the Back button fields to "Back"
Watch out for the controllers that are not given their own UINavigationItem automatically.