(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
}
最佳答案
希望我正确理解了您的问题:您想在 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/