双击UITabBarController时防止自动popToRootViewController

Nil*_*ect 33 iphone uitabbarcontroller uinavigationcontroller

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

有没有人碰到这个,如果有的话,你做了什么?我是否需要子类化UINavigationController并覆盖popToRootViewController或者是否有更简单的方法?

Sea*_*phy 64

使用tabBarController:shouldSelectViewController:UITabBarControllerDelegate协议的方法.

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
    return viewController != tabBarController.selectedViewController;
}
Run Code Online (Sandbox Code Playgroud)

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


小智 14

这就是我做的:

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController 
{

    if ([[tabBarController viewControllers] objectAtIndex:[tabBarController selectedIndex]] == viewController)

            return NO;

    return YES;

}
Run Code Online (Sandbox Code Playgroud)

问候


Har*_*ngh 5

更新斯威夫特 4.1

停止双击所有选项卡。

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

停止仅在一个特定选项卡上双击。这是第三个选项卡。

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))       
}}
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你...

谢谢!!!