【问题标题】:Prevent automatic popToRootViewController on double-tap of UITabBarController防止双击 UITabBarController 时自动 popToRootViewController
【发布时间】:2010-12-23 10:14:40
【问题描述】:

UITabBarController 的默认行为是在第二次点击特定选项卡时将包含的 UINavigationController 弹出到根视图控制器。我有一个特殊的用例,我希望它不能自动工作,我很难弄清楚如何防止这种情况。

有没有人遇到过这种情况,如果有,你做了什么?我需要继承 UINavigationController 并覆盖 popToRootViewController 还是有更简单的方法?

【问题讨论】:

    标签: iphone uinavigationcontroller uitabbarcontroller


    【解决方案1】:

    使用UITabBarControllerDelegate protocoltabBarController:shouldSelectViewController: 方法。

    - (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
        return viewController != tabBarController.selectedViewController;
    }
    

    不要忘记将标签栏控制器的委托设置为实际实现此委托方法的对象。

    【讨论】:

    • 太棒了。我不知道我是如何在委托协议中错过的。它就像一个魅力。谢谢!
    • 非常好。感谢分享!
    【解决方案2】:

    这就是我所做的:

    - (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController 
    {
    
        if ([[tabBarController viewControllers] objectAtIndex:[tabBarController selectedIndex]] == viewController)
    
                return NO;
    
        return YES;
    
    }
    

    问候

    【讨论】:

      【解决方案3】:

      更新 Swift 4.1

      停止双击所有标签。

      extension TabBarController: UITabBarControllerDelegate {
      func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
          //for blocking double tap on all tabs.
          return viewController != tabBarController.selectedViewController
      }}
      

      仅在一个特定标签上停止双击。这是第三个标签。

      extension TabBarController: UITabBarControllerDelegate {
      func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
          //for blocking double tap on 3rd tab only
          let indexOfNewVC = tabBarController.viewControllers?.index(of: viewController)
          return ((indexOfNewVC != 2) ||
              (indexOfNewVC != tabBarController.selectedIndex))       
      }}
      

      希望对你有帮助...

      谢谢!!!

      【讨论】:

      • 别忘了把tab bar控制器的delegate设置成实际实现这个的对象
      【解决方案4】:

      这种行为有点奇怪,但在层次结构较深的情况下是一个方便的捷径!

      您可以实现以下 UITabBarControllerDelegate 方法来禁用此系统范围的快捷方式:

      #pragma mark -
      #pragma mark UITabBarControllerDelegate
      
      - (BOOL)tabBarController:(UITabBarController *)tbc shouldSelectViewController:(UIViewController *)vc {
          UIViewController *tbSelectedController = tbc.selectedViewController;
      
          if ([tbSelectedController isEqual:vc]) {
              return NO;
          }
      
          return YES;
      }
      

      【讨论】:

        【解决方案5】:

        这里是 Swift 3 版本:

        func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
            return viewController != tabBarController.selectedViewController
        }
        

        【讨论】:

          猜你喜欢
          • 2013-02-12
          • 1970-01-01
          • 2019-02-05
          • 2011-09-11
          • 2011-02-10
          • 2015-08-07
          • 2016-07-11
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多