简体   繁体   中英

swift ios tabBar didSelectItem present login screen

I am trying to display a login screen if the user clicks on tab 2 or tab 3.

I tried adding:

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
            }
        }
}

This will show the modal VC/login screen that I have as a segue from the rootVC. But the tab bar still segues to the clicked tab.

What I want to do is to stop the tab bar form fire a segue to the selected VC/clicked tab and instead only display the modal VC/login screen

If you are using UITabBarController , you could override shouldSelectViewController method of its delegate ( UITabBarControllerDelegate ). It is there for you to dynamically decide if you want to switch to a particular view controller or not:

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM