swift ios tabBar didSelectItem 呈现登录界面

swift ios tabBar didSelectItem present login screen

我正在尝试在用户单击选项卡 2 或选项卡 3 时显示登录屏幕。

我尝试添加:

override func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {

        if item.tag == 1 || item.tag == 2 {
            if LocalStore.getToken() == nil {
                self.performSegueWithIdentifier("loginSegue", sender: self)
                return
            }
        }
}

这将显示模态 VC/login 屏幕,我从 rootVC 开始使用它。但是标签栏仍然会转到点击的标签。

我想要做的是停止标签栏表单向选定的 VC/clicked 标签发送一个 segue,而是只显示模态 VC/login 屏幕

如果您正在使用 UITabBarController,您可以覆盖其委托 (UITabBarControllerDelegate) 的 shouldSelectViewController 方法。您可以动态决定是否要切换到特定的视图控制器:

func tabBarController(tabBarController: UITabBarController,
shouldSelectViewController viewController: UIViewController) -> Bool {
    guard 
        let tab = tabBarController.viewControllers?.indexOf(viewController) 
        where [1, 2].contains(tab) 
        else { return true }
    if LocalStore.getToken() == nil {

        /// Present the login screen here

        return false
    }
    return true
}