Search code examples
iosswiftuitabbarcontrollerswift3user-roles

How to reload tab bar items after user changes role


When tap a button, app user can switch between two roles, user and manager.

In my tabBarViewController's viewDidLoad method,

if userRole == "manager" {

            let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
            let managerHomeViewController = (mainStoryboard.instantiateViewController(withIdentifier: "ManagerHome")) as! UINavigationController
            let managerProfileViewController = (mainStoryboard.instantiateViewController(withIdentifier: "ManagerProfile")) as! UINavigationController
            self.viewControllers?.replaceSubrange(0...1, with: [managerHomeViewController,managerProfileViewController])
        }

to change first two tab bar items when loading the view.

In anotherViewController where user tap a button to change role,

MainTabBarViewController().aboveFunction()

After running this line, the tab bar items are not updated. It can only be updated to new tab bar items by force closing the app and relaunch it. How to reload tab bar items instantly after updating them?


Solution

  • I didn't find the solution to reload, but I eventually use two tab bar controller in storyboard to present two views for user and manager.

    And when tapping the button to change roles

    let homeViewController = (self.storyboard!.instantiateViewController(withIdentifier: "ManagerHomeTabBar")) as UIViewController
    self.present(homeViewController, animated: false, completion: nil)
    

    to switch to the correct tab bar view.