【问题标题】:How to pop to root after double tapping a tab bar item?双击标签栏项目后如何弹出到根目录?
【发布时间】:2016-03-30 08:27:47
【问题描述】:

通常,当UINavigationController 放置在UITabBarController 中时,导航控制器会弹出到根目录,并双击它所在的相应选项卡。如何在标签栏控制器和导航控制器之间使用UISplitViewController 达到相同的效果?理想情况下,它将通过视图控制器的子视图控制器递归并在它找到的所有导航控制器上调用popToRootViewController。我是否必须将自己的手势识别器添加到标签栏,因为它看起来不像有一个钩子可以知道用户何时双击标签?

【问题讨论】:

    标签: ios uinavigationcontroller uitabbarcontroller uisplitviewcontroller uitabbar


    【解决方案1】:

    我没有设置UIGestureRecognizer,而是简单地跟踪shouldSelectViewController 中的选定索引,并在didSelectViewController 中的主导航控制器上弹出到根目录,如果旧的选定索引与新的索引相同。

    【讨论】:

      【解决方案2】:

      在 Swift 4 中,它可以是这样的:

      class TabBarViewController: UITabBarController, UITabBarControllerDelegate {
      
          private var shouldSelectIndex = -1
      
          override func viewDidLoad() {
              super.viewDidLoad()
              delegate = self
          }
      
          func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
              shouldSelectIndex = tabBarController.selectedIndex
              return true
          }
      
          func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
              if shouldSelectIndex == tabBarController.selectedIndex {
                  if let splitViewController = viewController as? UISplitViewController {
                      if let navController = splitViewController.viewControllers[0] as? UINavigationController {
                          navController.popToRootViewController(animated: true)
                      }
                  }
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-11-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多