【发布时间】:2019-04-26 08:11:58
【问题描述】:
我正在使用 splitViewController 来显示主视图和详细视图。
当我点击一行时,详细视图会正确更新。
然后,当我在纵向视图中时,我折叠 splitview 详细视图,以便主列表项显示如下:
我遇到的问题是,如果我在上面显示的详细视图中旋转设备,当我在详细视图中时,旋转正确地返回到 splitView,但是,现在当我选择一行时,委托方法不更新详细视图。如果我从 splitView 开始并停留在该视图中,或者如果我从折叠视图开始并停留在该视图中,它似乎才有效。如果我旋转,那么委托方法似乎不起作用。
我找到了一篇之前的帖子,它展示了如何使用委托方法来更新详细视图,使用目标 C 代码,使用 didSelectRow 函数。我尝试使用以下 swift 代码复制此代码:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let navigationVC = UINavigationController()
var detailVC = TestsDetailAdvertVC()
if let tests = controller.fetchedObjects, tests.count > 0 {
//if there is, keep track of the test which is selected
selectedTest = tests[indexPath.row]
if let isCollapsed = splitViewController?.isCollapsed {
if isCollapsed {
//solves problem of navigating to the detail view in compact view
// on the iPhone (compact) the split view controller is collapsed
// therefore we need to create the navigation controller and detail controller
detailVC = self.storyboard!.instantiateViewController(withIdentifier: "detailVC") as! TestsDetailAdvertVC
navigationVC.setViewControllers([detailVC], animated: false)
self.splitViewController?.showDetailViewController(detailVC, sender: self)
detailVC.testToEdit = selectedTest
} else {
// if the split view controller shows the detail view already there is no need to create the controllers
// so we just pass the correct test using the delegate
// if the test variable is set, then it calls the showDetail function
delegate?.testToEdit = selectedTest
}
}
}
}
我认为不知何故,当使用一种或另一种方法更新详细视图时,它可以工作,但是当它来回切换时,它会停止工作。我想知道是否有人使用 swift 代码解决了这个问题,谁能给我举个例子。
注意:经过一些额外的搜索,我意识到splitViewController有一些委托方法,包括:
func primaryViewControllerForExpandingSplitViewController:
和
func primaryViewControllerForCollapsingSplitViewController:
和
splitViewController:separateSecondaryViewControllerFromPrimaryViewController:
我一直在摆弄这些方法,但到目前为止还无法让它们发挥作用,而且我还没有找到任何展示如何使用它们的示例的帖子。
谢谢。
【问题讨论】:
-
试试 Xcode 的 Master Detail 应用程序模板,看看它是如何工作的,仔细查看故事板以了解如何使用自适应转场。无需自动检查折叠状态。
标签: ios swift uisplitviewcontroller master-detail