【问题标题】:How to properly use didSelectRowAt in splitViewController on compact device in swift?如何快速在紧凑型设备上的 splitViewController 中正确使用 didSelectRowAt?
【发布时间】: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


【解决方案1】:

我想出了如何正确更新详细视图,使用之前帖子中的答案:

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 上,详细视图必须来自展开的导航控制器。然后当我正确旋转正确的细节视图控制器更新时。

【讨论】:

    猜你喜欢
    • 2013-09-13
    • 1970-01-01
    • 2012-07-28
    • 2010-10-07
    • 1970-01-01
    • 2019-02-14
    • 1970-01-01
    • 2022-12-15
    • 1970-01-01
    相关资源
    最近更新 更多