Kerim Khasbulatov
Kerim Khasbulatov

Reputation: 722

UITabbarController replace the last view controller

I need replace the last view controller, depending on the condition. Into TabBarController I have 4 view controllers, last view controller it's LoginVC. If I'm already logged in I need replace LoginVC to ProfileVC.

I try:

 func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
    if tabBarController.selectedIndex == 3 && !DefaultsManager.instance.getUserToken().isEmpty {
        showProfile()
        return false
    }
    return true
}

func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
    if tabBarController.selectedIndex == 3 && !DefaultsManager.instance.getUserToken().isEmpty {
        showProfile()
    }
}

 func showProfile() {
    let profile = ProfileVC.instantiateNCFromStoryboard()
    self.viewControllers?[3] = profile
}

But it's does't work for me.

screen with login screen enter image description here

screen profile screen with my condition

enter image description here

Upvotes: 0

Views: 414

Answers (1)

RajeshKumar R
RajeshKumar R

Reputation: 15788

In AppDelegate check user login status and change the last view controller and its tabBarItem

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    if let rootVC = self.window?.rootViewController as? UITabBarController, var viewControllers = rootVC.viewControllers {
        viewControllers.removeLast()
        if userLoggedIn {
            let profileVC = ProfileVC()
            //profileVC.tabBarItem = UITabBarItem(title: nil, image: UIImage(named: "imgName"), selectedImage: UIImage(named: "imgName"))
            profileVC.tabBarItem = UITabBarItem(title: "Profile", image: nil, selectedImage: nil)
            viewControllers.append(profileVC)
        } else {
            let loginVC = LoginVC()
            //loginVC.tabBarItem = UITabBarItem(title: nil, image: UIImage(named: "imgName"), selectedImage: UIImage(named: "imgName"))
            loginVC.tabBarItem = UITabBarItem(title: "Login", image: nil, selectedImage: nil)
            viewControllers.append(loginVC)
        }
        rootVC.viewControllers = viewControllers
    }
    return true
}

In LoginVC upon successfull login, remove LoginVC from tabBarController and add ProfileVC

@objc func loginBtnAction(_ sender: UIButton) {
    if var viewControllers = self.tabBarController?.viewControllers {
        viewControllers.removeLast()
        let profileVC = ProfileVC()
        //profileVC.tabBarItem = UITabBarItem(title: nil, image: UIImage(named: "imgName"), selectedImage: UIImage(named: "imgName"))
        profileVC.tabBarItem = UITabBarItem(title: "Profile", image: nil, selectedImage: nil)
        viewControllers.append(profileVC)
        self.tabBarController?.viewControllers = viewControllers
    }
}

In Logout button action from ProfileVC, remove ProfileVC from tabBarController and add LoginVC

@objc func logoutBtnAction(_ sender: UIButton) {
    if var viewControllers = self.tabBarController?.viewControllers {
        viewControllers.removeLast()
        let loginVC = LoginVC()
        //loginVC.tabBarItem = UITabBarItem(title: nil, image: UIImage(named: "imgName"), selectedImage: UIImage(named: "imgName"))
        loginVC.tabBarItem = UITabBarItem(title: "Login", image: nil, selectedImage: nil)
        viewControllers.append(loginVC)
        self.tabBarController?.viewControllers = viewControllers
    }
}

Upvotes: 1

Related Questions