swiftxcode11side-menurootviewcontrolleruiscenedelegate

Set rootViewController programmatically not working, picking initial view controller from storyboard only in Xcode 11


Trying to change the root view controller programmatically, initially in storyboard as initial view controller is checked. Every time same viewController is opened which is set up as initialViewController in storyboard. Please guide. Sharing below what code tried so far:

let isLoggedIn = UserDefaults.standard.bool(forKey: kIsLoggedIn)
        if isLoggedIn{
            //Welcome Screen for Touch ID
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
           // let rootVC = storyboard.instantiateViewController(withIdentifier: "WelcomeViewController") as! WelcomeViewController
            let rootVC = storyboard.instantiateViewController(withIdentifier: "DashboardVC") as! DashboardVC
            setRoot(rootVC: rootVC)
        }
        else{
            //Login Screen
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let rootVC = storyboard.instantiateViewController(withIdentifier: "RegisterViewController") as! RegisterViewController
            setRoot(rootVC: rootVC)
        }

func setRoot(rootVC: UIViewController){
    let navController = UINavigationController.init(rootViewController: rootVC)
    navController.isNavigationBarHidden = true
    //if let window = self.window{
        window?.rootViewController = rootVC
        window?.makeKeyAndVisible()
    //}
}

Secondly, trying to change the rootViewController and trying to add MFSideMenu after login but not working swift. Please find it's code below:

 func loadDashBoard(storyboard : UIStoryboard){
    let vc = storyboard.instantiateViewController(withIdentifier: "MenuNavigationController")
    let sideMenuVC = storyboard.instantiateViewController(withIdentifier: "SideMenuViewController") as! SideMenuViewController
    let container : MFSideMenuContainerViewController = MFSideMenuContainerViewController.container(withCenter: vc, leftMenuViewController: sideMenuVC, rightMenuViewController: nil)
    let navController = UINavigationController.init(rootViewController: container)
    navController.isNavigationBarHidden = true

        window?.rootViewController = navController
        window?.makeKeyAndVisible()
    }

All the above written code in AppDelegate.


Solution

  • Got the answer by doing some more google, i mean stackoverflow.

    class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    
    var window: UIWindow?
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    
    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
      guard let windowScene = scene as? UIWindowScene else { return }
      let vc = storyboard.instantiateViewController (withIdentifier: "Primary") as! ViewController
      window = UIWindow(windowScene: windowScene)
      window?.rootViewController = vc
      window?.makeKeyAndVisible()
    }
    

    SceneDelegate is used for this purpose since iOS 13 not AppDelegate. Here is the link SceneDelegate example