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

二次信任 提交于 2021-01-29 16:20:35

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/59563489/set-rootviewcontroller-programmatically-not-working-picking-initial-view-contro

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!