Rootview controller is not working with tabbar controller Swift 4.2

766 Views Asked by At

Im having login pages where im trying to push each ViewController and after successful verification im trying to set root view using code

UIApplication.shared.delegate?.window??.rootViewController = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "Some") as! Some

Above code is not working and for logout also im setting the root view controller, but those are not working. Please help me out. Thanks in advance.

Login 1st page(navigationController.pushVc)--> Success Verification (setting root view controller) --> Logout(setting root view controller).

I have given identifiers to both tabbar controller and navigation controller. Tried in many ways but could not find solution

Tried this link also https://fluffy.es/how-to-transition-from-login-screen-to-tab-bar-controller/

Above link is the same which im trying.But can't do in my project.

1

There are 1 best solutions below

0
On

I am copying my method here to switch rootViewController after login and logout. I have written this method in AppDelegate:

public func setRootVC(){
    
    if UserDefaults.standard.object(forKey: Strings.LOGGED_IN) != nil {
        let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
        let tabbarVC = storyboard.instantiateViewController(withIdentifier: ViewControllerIdentifiers.TabbarController)
        
        self.window?.rootViewController = tabbarVC
        self.window?.makeKeyAndVisible()
    }
    else{
        let storyboard = UIStoryboard(name: "Profile", bundle: Bundle.main)
        let loginNavController = storyboard.instantiateViewController(withIdentifier: ViewControllerIdentifiers.LoginNavController) as? UINavigationController
        
        self.window?.rootViewController = loginNavController
        self.window?.makeKeyAndVisible()
    }
}

Calling this method from my viewController:

@IBAction func signoutBtnClicked(_ sender: Any) {
    
    if UserDefaults.standard.object(forKey: Strings.LOGGED_IN) != nil {
        UserDefaults.standard.removeObject(forKey: Strings.LOGGED_IN)
    }
    self.appDelegate.setRootVC()
}

To access 'self.appDelegate I have added this extension in my viewController

extension UIViewController {
var appDelegate: AppDelegate {
    return UIApplication.shared.delegate as! AppDelegate
}

}