xcode 拆分视图控制器:如何将主视图控制器连接到细节视图控制器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24813442/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 05:13:26  来源:igfitidea点击:

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

xcodeswiftuisplitviewcontrollermaster-detaildidselectrowatindexpath

提问by kmiklas

(Xcode6-beta3, Swift, iOS8, iPad)

(Xcode6-beta3、Swift、iOS8、iPad)

In an iPad split-view controller, how do I link the Master View Controller to the Detail View Controller?

在 iPad 拆分视图控制器中,如何将主视图控制器链接到详细视图控制器?

In other words, when the user taps on an item on the left, how do I change the view on the right?

换句话说,当用户点击左侧的项目时,如何更改右侧的视图?

I know that in didSelectRowAtIndexPath, I need to call a method... but how do I call a method in the Detail View Controller from the Master View Controller?

我知道在 didSelectRowAtIndexPath 中,我需要调用一个方法……但是如何从主视图控制器调用细节视图控制器中的方法?

Example

例子

Imagine an app to display information on different types of cheeses. We begin by dragging a split-view controller onto the storyboard. A table of items in the master view on the left is set up to read as follows.

想象一个应用程序来显示有关不同类型奶酪的信息。我们首先将拆分视图控制器拖到情节提要上。左侧主视图中的项目表设置如下。

  • Swiss
  • Cheddar
  • Brie
  • 瑞士人
  • 切达干酪
  • 布里干酪

On the right, there is simply a Web View inside of the detail view controller, named cheeseViewController. Therein, HTML documents about the selected cheese will be displayed.

在右侧,详细信息视图控制器内部只有一个 Web 视图,名为cheeseViewController。其中,将显示有关所选奶酪的 HTML 文档。

An IBOutlet is wired from the web view into cheeseViewController, and a method named 'changeCheese' is set up in the Detail View Controller delegate to swap out the document.

一个 IBOutlet 从 web 视图连接到cheeseViewController,并且在细节视图控制器委托中设置了一个名为“changeCheese”的方法来换出文档。

How can I make a tap on "Cheddar" change the information in the detail view?

如何点击“Cheddar”更改详细信息视图中的信息?

EDIT: Do I have to modify my AppDelegate.swift file? Using a Master-Detail template, I tried the following, with no luck:

编辑:我必须修改我的 AppDelegate.swift 文件吗?使用 Master-Detail 模板,我尝试了以下操作,但没有成功:

func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {
    // Override point for customization after application launch.

    let splitViewController = self.window!.rootViewController as UISplitViewController
    let navigationController = splitViewController.viewControllers[splitViewController.viewControllers.count-1] as UINavigationController
    splitViewController.delegate = navigationController.topViewController as Paragraph

    return true
}

enter image description here

在此处输入图片说明

回答by Lennart

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

我希望我正确理解了您的问题:您想在 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:

当您在 XCode 6 Beta 3 中创建新的 Master-Detail-View 应用程序时,您的 DetailViewController.Swift 文件中将有一个名为“detailItem”的变量:

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

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

您可以在 MasterViewController.Swift 文件中的以下函数中设置此 detailItem:

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

(假设您已将视图与带有标识符的 segue 链接起来:“yourSegueIdentifier”和一个名为“yourCheeseArrayWithDetailInformation”的 detailinfo 数组)

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

上面提到的DetailView中的函数“configureView”现在可以访问你的detailItem,其中包含“cheeese”的内容

I hope this helps you.

我希望这可以帮助你。

回答by marcb

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.

为什么不直接在 Master 中从 didSelectRowAtIndexPath 发布一个通知,并在您的视图中最有可能在 viewDidLoad 中添加一个观察者。您还可以使用闭包在观察者方法中处理选择器。

回答by HenryRootTwo

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

如果你没有创建一个主从应用程序(所以你没有detailItem),你可以使用这个:

if let
    mySplitViewController = splitViewController,
    detailView = mySplitViewController.childViewControllers.last as? DetailViewController {
        // do something with it
    }