如何将主视图控制器

如何将主视图控制器

本文介绍了拆分视图控制器:如何将主视图控制器连接到详细视图控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(Xcode6-beta3,Swift,iOS8,iPad)

(Xcode6-beta3, Swift, iOS8, iPad)

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

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

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

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

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

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?

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

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.

  • 瑞士
  • 切达乳酪
  • 布里

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

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.

将IBOutlet从Web视图连接到cheeseViewController,并在Detail View Controller委托中设置了名为"changeCheese"的方法以换出文档.

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.

如何在切达"上轻按以更改详细信息视图中的信息?

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

是否必须修改AppDelegate.swift文件?使用Master-Detail模板,我尝试了以下尝试,但是没有运气:

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

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
}

推荐答案

我希望我正确理解了您的问题:您想在Detailview中显示所选奶酪的详细信息.

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

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

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()
    }

您可以通过以下功能在MasterViewController.Swift文件中设置此detailItem:

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
   }
}

(假设您已将视图与标识符为"yourSegueIdentifier"和名为"yourCheeseArrayWithDetailInformation"的detailinfo数组的segue链接在一起)

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

DetailView中的上述函数"configureView"现在可以访问您的detailItem,其中包含"cheeese"的内容

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

希望对您有帮助.

这篇关于拆分视图控制器:如何将主视图控制器连接到详细视图控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 01:11