Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift 4 - How to override tab bar will open view controller

Is it possible that when a user clicks on a Tab bar item that I can override this in the UITabBarController , where I then check a UserDefault which then decides whether I show the view or return and they stay on their current view ?

It would be something like this:

override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
    if item.image == UIImage(named: "TabProfile")
    {
        // Profile tab selected
        if !loginController.isUserLogged()
        {
            // Not logged in...
            showLoginView()  

            // Following line doesn't work...
            tabBarController?.selectedIndex = selectedIndex
        }
    }
}

If possible I want to perform this check and if false then actually prevent the view from even reaching viewDidLoad.

Thanks.

like image 566
Niall Kiddle Avatar asked Oct 16 '25 19:10

Niall Kiddle


1 Answers

I guess you need

func tabBarController(_ tabBarController: UITabBarController, 
              shouldSelect viewController: UIViewController) -> Bool {
   if let ind = tabBarController.viewControllers!.index(of:viewController) , ind == 2 { // suppose profile is 2
      // 
       if userNotLogged { 
         // present modal login view 
         return false
       } 
   } 
  return true
}
like image 59
Sh_Khan Avatar answered Oct 18 '25 13:10

Sh_Khan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!