ios - 如何在紧凑型设备上快速正确使用 splitViewController 中的 didSelectRowAt ?

标签 ios swift uisplitviewcontroller master-detail

我使用 splitViewController 来显示主视图和详细 View 。

enter image description here

当我点击一行时,详细信息 View 会正确更新。

然后,当我处于纵向 View 时,我会折叠 Split View详细信息 View ,以便主列表项显示如下: enter image description here

当我点击一行时,我正确地移动到详细 View ,如图所示: enter image description here

我遇到的问题是,如果我在上面显示的详细 View 中旋转设备,当我在详细 View 中时,旋转会正确返回到 splitView,但是,现在当我选择一行时,委托(delegate)方法不会更新详细信息 View 。只有当我从 splitView 开始并停留在该 View 中,或者从折叠 View 开始并停留在该 View 中时,它似乎才起作用。如果我旋转,那么委托(delegate)方法似乎不起作用。

enter image description here

我发现了之前的一篇文章,其中展示了如何使用委托(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 :

In UISplitViewController, can't make showDetailViewController:sender: push onto detail navigationController

我解决问题的代码是使用 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/

相关文章:

ios - 使用 Swift 从日期选择器开始倒计时

ios - 使用来自不同函数的 tableviewcell

ios - 在 iPad 中长按 Tableview 单元格会崩溃

ios - Swift调用随机函数

uisplitviewcontroller - 无法使 UIPopover 以编程方式出现

authentication - 如何为 Split View Controller 创建启动页面

ios - 以编程方式将借记卡或信用卡添加到 ApplePay

ios - 初始 View Controller 无响应,控制台中没有错误

ios - UITextView 不更改 textColor 属性

ios - 如何在基于 splitview 的 iPad 应用程序中快速查看文档