禁用点按当前标签(UITabBarController)iPhone App

时间:2012-04-07 12:34:51

标签: iphone ios uitabbarcontroller

目前,点击同一个标签(用户正在使用),该应用程序移动到该标签的第一页。

我想在用户当前正在使用的标签上禁用点击事件。

任何提示?

2 个答案:

答案 0 :(得分:13)

您尝试了tabBarController:shouldSelectViewController:委托方法吗?我希望能帮助你。

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

    id currentViewController = tabBarController.selectedViewController;
    return (viewController != currentViewController);
}

如果标签栏控制器的所有视图控制器都是UINavigationControllers,您应该这样做。

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

    id nextVC = [(UINavigationController *)viewController topViewController];
    id currentVC = [(UINavigationController *)tabBarController.selectedViewController topViewController];
    return (nextVC != currentVC);
}

对于Swift 4,委托方法如下所示:

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

答案 1 :(得分:0)

使用如下所示它将起作用

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