Split View Controller: How to connect Master View Controller to Detail View Controller?

对着背影说爱祢 提交于 2019-12-07 00:19:29

I hope I understood your problem correctly: You would like to show the detail information of a selected cheese in your Detailview.

When you create a new Master-Detail-View application in XCode 6 Beta 3, there will be a variable called "detailItem" in your DetailViewController.Swift file:

var detailItem: AnyObject? {
    didSet{
        self.configureView()
    }

You set this detailItem in your MasterViewController.Swift file in the following function:

override func prepareForSegue(segue: UIStoryBoardSegue, sender: AnyObject?){
   if segue.identifier == "yourSegueIdentifier" {
      let indexPath = self.tableView.indexPathForSelectedRow()
      let cheeese = yourCheeseArrayWithDetailInformation[indexPath.row]
      (segue.destinationViewController as DetailViewController).detailItem = cheeese
   }
}

(Assuming, that you have linked the views with a segue with the identifier: "yourSegueIdentifier" and an array of detailinfo called "yourCheeseArrayWithDetailInformation")

The above mentioned function "configureView" in the DetailView can now access your detailItem, which contains the contents of "cheeese"

I hope this helps you.

Why don't you just post a Notification from didSelectRowAtIndexPath in your Master and add an observer in your Detail View most likely inside your viewDidLoad. You also can handle the selector within the observer method with closure.

If you didn't create a master-detail app (so you have no detailItem), you might use this:

if let
    mySplitViewController = splitViewController,
    detailView = mySplitViewController.childViewControllers.last as? DetailViewController {
        // do something with it
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!