0
votes

I have created a UITabBarController and three view controllers in a storyboard as its tab bar items.

What i want is to check if a user is logged in using PFU.current() when a user press third tab bar menu and direct to log-in page for non-logged in users, and profile page for logged in users.

I am struggling with assigning ViewController created in storyboard to the third tab bar item menu programatically.

I have tried

let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
                let userProfileVC = storyBoard.instantiateViewController(withIdentifier: "UserProfileViewController") as! UserProfileViewController
                self.viewControllers?[2] = userProfileVC
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
        switch item.tag {
        case 0:
            print(0)
        case 1:
            print(1)
        case 2:
            if (PFUser.current() != nil) {

                let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
                let userProfileVC = storyBoard.instantiateViewController(withIdentifier: "UserProfileViewController") as! UserProfileViewController
                self.viewControllers?[2] = userProfileVC
            }else{
                print("not logged in")

            }
        default:
            break
        }   
    }

When a third tab bar item is pressed, it should check login first and direct to a page according to the condition. (Unlogged-in: loginPage, logged-in: profilePage)

1

1 Answers

0
votes

It's better to do it early

class Tab:UITabBarController { 
   override func viewDidLoad() {
       super.viewDidLoad() 
           var vc3:UIViewController! 
           if (PFUser.current() != nil) { 
            let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
            let userProfileVC = storyBoard.instantiateViewController(withIdentifier: "UserProfileViewController")!
            vc = userProfileVC
        }else{
            print("not logged in")
             vc = ///// 
        } 
        self.viewControllers = [vc1,vc2,vc3]
     } 
}