我使用 splitViewController 来显示主视图和详细 View 。
当我点击一行时,详细信息 View 会正确更新。
然后,当我处于纵向 View 时,我会折叠 Split View详细信息 View ,以便主列表项显示如下:
我遇到的问题是,如果我在上面显示的详细 View 中旋转设备,当我在详细 View 中时,旋转会正确返回到 splitView,但是,现在当我选择一行时,委托(delegate)方法不会更新详细信息 View 。只有当我从 splitView 开始并停留在该 View 中,或者从折叠 View 开始并停留在该 View 中时,它似乎才起作用。如果我旋转,那么委托(delegate)方法似乎不起作用。
我发现了之前的一篇文章,其中展示了如何使用委托(delegate)方法通过使用 didSelectRow 函数的 Objective C 代码来更新详细信息 View 。我尝试使用以下快速代码复制此代码:
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
}
}
}
}
我认为,当使用一种或另一种方法来更新详细 View 时,它会起作用,但当它来回切换时,它会停止工作。我想知道是否有人使用快速代码解决了这个问题,可以给我举一个例子。
注意:经过一些额外的搜索,我意识到 splitViewController 有一些委托(delegate)方法,包括:
函数primaryViewControllerForExpandingSplitViewController:
和
函数primaryViewControllerForCollapsingSplitViewController:
和
splitViewController:separateSecondaryViewControllerFromPrimaryViewController:
我一直在摆弄这些方法,但到目前为止还无法让它们发挥作用,而且我还没有找到任何展示它们如何使用的示例的帖子。
谢谢。
最佳答案
我使用之前帖子中的答案找到了如何正确更新详细信息 View :
我解决问题的代码是使用 swift 代码更新的:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
var detail = UINavigationController()
var testVC = 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 {
//in collapsed view, the correct detail view controller is not
//yet substantiated, so we need to substantiate it
testVC = self.storyboard?.instantiateViewController(withIdentifier: "detailVC") as! TestsDetailAdvertVC
detail.setViewControllers([testVC], animated: true)
testVC.testToEdit = selectedTest
} else {
//in expanded view, the correct view controller needs
//to be identified, using the appropriate view controller for
//the splitview controller
let vc = self.splitViewController?.viewControllers[1]
//which is a navigation controller
if vc is UINavigationController {
detail = vc as! UINavigationController
//which we then use to identify the correct detail view
testVC = detail.viewControllers.first as! TestsDetailAdvertVC
testVC.testToEdit = selectedTest
}
}
}
}
self.splitViewController?.showDetailViewController(detail, sender: self)
}
关键的解决方案是在折叠的 splitviewcontroller 上,必须从 Storyboard实例化详细 View 。但是,在扩展的 splitviewcontroller 上,详细 View 必须来自扩展的导航 Controller 。然后,当我旋转正确的详细 View Controller 时, Controller 会正确更新。
关于ios - 如何在紧凑型设备上快速正确使用 splitViewController 中的 didSelectRowAt ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53451355/