How to name a back button in UISplitViewController

流过昼夜 提交于 2019-12-12 10:36:37

问题


I have UITableViewController (its name is News) and UIViewController (its name is DetailViewController) and UISplitViewController. I want it to show a back button when I use an iPad in portrait orientation. I made the button but I cannot name it. I wrote following code

detailController.navigationItem.leftBarButtonItem = self.splitViewController?.displayModeButtonItem()
                detailController.navigationItem.leftItemsSupplementBackButton = true
                detailController.navigationItem.leftBarButtonItem?.title = navigationController?.topViewController.title

But it doesn't show the name of the button. I see only the arrow (the arrow works).

I also tried the following in my UITableViewController(News) but it didn't help me

I use two segues for different devices with this code.

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
        var screen = UIScreen.mainScreen().currentMode?.size.height
        if (UIDevice.currentDevice().userInterfaceIdiom == UIUserInterfaceIdiom.Pad) || screen >= 2000 && UIDevice.currentDevice().orientation.isLandscape == true  && (UIDevice.currentDevice().userInterfaceIdiom == .Phone){
            performSegueWithIdentifier("showDetailParse", sender: nil)
            self.dismissViewControllerAnimated(true, completion: nil)
        } else if (UIDevice.currentDevice().userInterfaceIdiom == .Phone) {
            performSegueWithIdentifier("showParse", sender: nil)
        }
    }

My result on an iPad

My result on an iPhone


回答1:


Thanks to Paul Hegarty and his invaluable lectures at Stanford University and available on iTunes U... in this case his 2013 lectures under the title "Developing iOS 7 Apps for iPhone and iPad" and specifically "Lecture 11 Table View and the iPad".

If you're using storyboards, then:

  • Open your main storyboard and select the Navigation Controller that links to the Master View Controller in your Split View Controller group;
  • Open the Inspector;
  • Under the heading View Controller, against the property Title, enter the words that you would like to appear alongside the "Back" button chevron.

See screenshot of Master Detail Xcode template set up with a Split View Controller...

If you're instantiating views in code, then:

  • obtain a reference to the Navigation Controller for the Master View controller;
  • set the title property of that Navigation Controller with the NSString of words that you would like to appear alongside the "Back" button chevron.

As an aside, I would highly recommend implementation of Auto Layout and Size Classes, that you remove the text for the Back Button property and let size classes determine the appropriate words for your Back Button.

For example, as per the question...




回答2:


The Solution:

Here is the way to fix the issue with the detail view controller's back button:

  • For any view controller that gets pushed onto the primary navigation controller's stack, set that view controller's title. (Either in its viewDidLoad: method or in the pushing view controller's prepareForSegue:sender: method.)
  • Set the primary navigation controller's title in the child view controller's viewDidLoad: method.

For example, in MasterViewController.m:

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setTitle:@"Foo"];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    [[self navigationController] setTitle:[self title]];
}

This will keep the detail view controller's back button title in sync with the top primary view controller's title.

What Is Going On:

UINavigationController, its rootViewController, and UINavigationItem each have a title property.

Note that the back button shown for a current view controller is actually the previous view controller's backButtonItem. (See Figure 1-7 Navigation bar structure)

A UINavigationController will automatically inherit the value of the title of its root view controller, but will not automatically inherit the title of any other controller that gets pushed onto its stack. This is why, by default, the back button of the detail view controller will always show the title of the primary navigation controller's root view controller. You might allocate, initialize, and push multiple child view controllers, but only one navigation controller is allocated and initialized for each side of a standard split view controller.

Additionally, a view controller's navigationItem's title property (whose value will appear in the label in the center of the navigation bar) does not inherit its value from the navigation controller, but from the view controller itself. If you set the view controller's title property to "Bar", and the containing navigation controller's title to "Foo", the label displayed in the center of the navigation bar will say "Bar".



来源:https://stackoverflow.com/questions/31318847/how-to-name-a-back-button-in-uisplitviewcontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!