breaktop
breaktop

Reputation: 2029

How to allow a Tab Bar Item to have multiple view controllers

I've been stuck on this problem for a few days now and any help would be appreciated.

I have a UITabBarController that has four TabBarItems.

I want to determine what view controller is displayed to the user depending on whether the user is logged in.

For example when the user is not logged in, I want to display different login screen depending on which TabBarItem is selected - each login will look different. When the user is logged in I want to display normal content the selected TabBarItem

I was thinking of having multiple ViewControllers for each tab bar item. When a user selects a TabBarItem, determine if the user is logged in and then display the correct ```ViewController. I'm not sure if this is the right approach and how to achieve this behaviour.

Update: This is the solution I have come up with but was wondering what everyone thinks - whether it's the right approach. This is what my Storyboard looks like: enter image description here

The idea is when the user is logged in I would show the login view controllers and when the user is not logged in I would show the logged out view controllers.

I would be using the self.present(...) to change to and from the login and logout ViewControllers.

Upvotes: 0

Views: 1742

Answers (1)

Cœur
Cœur

Reputation: 38667

You can change your displayed tabs with setViewControllers. Simply set the array of desired viewControllers when user logs in or logs out.

class TabBarController: UITabBarController {
    func onMyLogin() {
        setViewControllers(myNormalViewControllers, animated: false)
    }
    func onMyLogout() {
        setViewControllers(myUnidentifiedViewControllers, animated: false)
    }
}

Upvotes: 1

Related Questions