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

标签 xcode swift uisplitviewcontroller master-detail didselectrowatindexpath

(Xcode6-beta3、Swift、iOS8、iPad)

在 iPad Split View Controller 中,如何将主视图 Controller 链接到详细 View Controller ?

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

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

例子

假设有一款应用可以显示不同类型奶酪的信息。我们首先将一个 Split View Controller 拖到 Storyboard上。左侧主视图中的项目表设置如下。

  • 瑞士
  • 切达干酪
  • 布里干酪

在右侧,详细 View Controller 中只有一个 Web View ,名为 cheeseViewController。其中,将显示有关所选奶酪的 HTML 文档。

IBOutlet 从 Web View 连接到 cheeseViewController,并在详细 View Controller 委托(delegate)中设置名为“changeCheese”的方法来换出文档。

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

编辑:我必须修改我的 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

最佳答案

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

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

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

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

(假设,您已将 View 与带有标识符的 segue 链接:“yourSegueIdentifier”和名为“yourCheeseArrayWithDetailInformation”的详细信息数组)

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

希望对你有帮助

关于xcode - Split View Controller : How to connect Master View Controller to Detail View Controller?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24813442/

相关文章:

ios - 如何知道我的 UITableView 中没有选择任何行?

iphone - 如何在 splitViewController 中的 RootViewController 中将页脚图像添加到 UITableView

iphone - 如何在横向模式下从底部滑动UIView

xcode - 快速浏览一下 Swift 中的 NSView

swift - NSBezierPath 绘制方向错误

uikit - 是否可以在 UISplitViewController 上隐藏导航栏?

iphone - 应用程序不会在 iPhone 模拟器上旋转

ios - 当我在应用程序中按创建帐户时,它会在没有任何信息的情况下创建帐户

ios - 将 CURL 转换为 URLRequest

ios - 将 UIButtons 分组为一个层并水平居中