swift ios tabBar didSelectItem现在登录界面

时间:2016-05-21 14:17:55

标签: ios swift uitabbarcontroller segue

如果用户点击标签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
            }
        }
}

这将显示我从rootVC作为segue的模态VC /登录屏幕。但是标签栏仍然会分割到点击的标签页。

我想要做的是停止标签栏形成一个segue到选定的VC /点击标签,而只显示模态VC /登录屏幕

1 个答案:

答案 0 :(得分:2)

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

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
}