【问题标题】:Preventing a custom UITabBarController from changing Tab防止自定义 UITabBarController 更改选项卡
【发布时间】:2014-03-14 16:04:25
【问题描述】:

如何根据条件防止标签被点击?我尝试了几件事,但我可能错过了一些基础知识。

我有一个实现 UITabBarController 的 UITabBarCustomController。在 viewDidLoad 中,根据条件,我将 selectedIndex 强制为 1,这有效。

不过,我无法阻止用户离开此选项卡。我尝试在与我的 UITabBar 链接的另一个 ViewController 上设置一个 UITabBarControllerDelegate,在那里我实现了- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController,最后我还尝试在我的自定义 UITabBarCustomController 上实现这个委托和方法。但是在这两种情况下,当我单击另一个选项卡时,什么都没有发生,用户可以访问该选项卡(这些方法中的 NSLog 不会在调试中显示)。

有什么想法吗?谢谢。

// 编辑:委托方法中的测试代码:

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
    NSLog(@"test");
    return NO;
}

【问题讨论】:

  • shouldSelectViewController: 方法中有什么? (在您的问题中添加相关代码
  • 我希望你在所有其他 viewController 的委托方法中返回 NO。
  • 我编辑了我的帖子。目前,即使 NSLog 也没有出现在调试中。条件(此处未显示)有效。 @AnindyaSengupta 是的,该方法在 UITabBarController 处理的每个 viewController 中实现,并且对所有人都是一样的。
  • 那么你最好让它工作。 ...检查您是否在 UITabBarController 对象上设置了委托(self 主要是)...只需发布 UITabBarController .h 和 .m。你是通过编程还是通过 xib/storyboard 完成的?
  • @staticVoidMan 不,我没有 :) “错过了一些基础知识”呃。委托未在其他视图控制器中设置。我在其中添加了 self.tabBarController.delegate = self 并且可以正常工作,谢谢。

标签: ios uitabbarcontroller


【解决方案1】:

这是您可以停止/防止标签栏项目在标签栏项目点击上切换标签的方法

对于 Swift 3.0

确保您已实现 UITabBarControllerDelegate 并将 UITabbarControllerdelegate 设置为 自我

然后在你的控制器中覆盖这个委托

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

    if viewController == tabBarController.viewControllers?[2] {
        return false
    } else {
        return true
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-29
    • 2020-01-21
    • 2011-08-28
    • 1970-01-01
    • 2011-11-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多