How to change the UINavigationController back butt

2020-01-27 02:22发布

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.

17条回答
再贱就再见
2楼-- · 2020-01-27 02:54

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:

Set Back Button Title of child view controller form parent view controller

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).

查看更多
放荡不羁爱自由
3楼-- · 2020-01-27 02:56

Swift 3:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let backItem = UIBarButtonItem()
    backItem.title = "Back"
    navigationItem.backBarButtonItem = backItem // This will show in the next view controller being pushed
}

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:

查看更多
Deceive 欺骗
4楼-- · 2020-01-27 02:58

If you wish to do this programmatically, it can be done like this:

Objective-C

UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:@"Custom"
                                                             style:UIBarButtonItemStyleBordered
                                                            target:nil
                                                            action:nil];

[self.navigationItem setBackBarButtonItem:backItem];

Swift

let backItem = UIBarButtonItem(title: "Custom", style: .Bordered, target: nil, action: nil)
navigationItem.backBarButtonItem = backItem

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.

enter image description here

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.

查看更多
Ridiculous、
5楼-- · 2020-01-27 02:58

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:

self.navigationItem.backBarButtonItem?.title = "Your customized back title"

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,

"nnnnnnn.title" = "localized string";

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.

查看更多
Summer. ? 凉城
6楼-- · 2020-01-27 02:59

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.

查看更多
We Are One
7楼-- · 2020-01-27 02:59

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.

查看更多
登录 后发表回答