【问题标题】:How to get to the deepest viewController in a complex hierarchy如何到达复杂层次结构中最深的 viewController
【发布时间】:2020-08-05 19:29:37
【问题描述】:

我的层次结构看起来像这样:

VC 层次结构

  1. UITabBarController

1.1 ViewController 和 2 个容器视图

1.1.1。 UINavigationController

1.1.1.1。 UITableViewController

1.1.1.1.1 UITableViewController

1.2 ViewController 和 2 个容器视图

1.2.1。 UINavigationController

1.2.1.1。 UITableViewController

1.2.1.1.1 UITableViewController

想要的解决方案

我想在选择tabBar时拥有最深的VC PopToRoot。This post explains how

我已经设法在调试器中找到了正确的 VC,但是在编写代码来获取它时,Xcode 抱怨 Segmentation Fault 11

代码

func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController ) -> Bool {

if tabBarController.selectedViewController === viewController {

if let nc = viewController.children.first(where: { $0.isKind(of: UINavigationController.self)}) as? UINavigationController {

if let handler = nc.children.first(where: { $0.isKind(of: TabBarReselectHandling.self )}) as? TabBarReselectHandling {
handler.handleReselect()
            }
        return true
    }

只要我注释掉 if let handler 行,Xcode 就会显示错误: An internal error occured. Source editor functionality is limited. Attempting to restore...

这一行适用于一个 tabBarItem,但不是全部,因为 VC 1.1 和 1.2 中的两个 containerViews 的顺序可能不同

let handler = viewController.children[1].children[1] as? TabBarReselectHandling

关于如何安全地找到我最深的 VC 有什么建议吗?

【问题讨论】:

    标签: swift


    【解决方案1】:

    您可以检查selectedIndex 并将viewController 转换为UINavigationController,然后对其执行popToRootViewController(animated:) 以达到此结果。

    func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
        if selectedIndex == 0, let navVC = viewController as? UINavigationController { // replace 0 with your value
            navVC.popToRootViewController(animated: true)
        }
    }
    

    注意:为此,您的 func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController ) -> Bool 方法的实现应该被删除。

    【讨论】:

      【解决方案2】:

      试试这个 Swift 4 版本

              if (navigationController?.viewControllers.filter { $0 is YourFirstViewController }.first != nil)
      
              {
                  let dashboardVC = navigationController!.viewControllers.filter { $0 is YourFirstViewController }.first
                  navigationController!.popToViewController(dashboardVC!, animated: true)
              }
                  
              else if (navigationController?.viewControllers.filter { $0 is YourSecondViewController }.first != nil)
              {
                  let dashboardVC = navigationController!.viewControllers.filter { $0 is YourSecondViewController }.first
                  navigationController!.popToViewController(dashboardVC!, animated: true)
              }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-01
        • 1970-01-01
        相关资源
        最近更新 更多