如何更改 swift 中的 rootViewController?

How to change rootViewController in swift?

我正在开发一个没有登录注销功能的应用程序。当用户打开应用程序时,他会看到主屏幕。现在在设置页面中有一个名为 Pin Lock 应用程序的选项。当用户 select 此选项出现 PinScreen(如 OTP 屏幕)时,然后用户设置 Pin。之后,当用户关闭应用程序时,Pin 锁定屏幕首先要求用户输入 pin。 那我该怎么做呢?

if UserDefaults.standard.bool(forKey: "LoggedIn") == false {

        let story = UIStoryboard(name: "Main", bundle:nil)
        let vc = story.instantiateViewController(withIdentifier: "EnterPin") as! UINavigationController
        UIApplication.shared.windows.first?.rootViewController = vc
        UIApplication.shared.windows.first?.makeKeyAndVisible()

        let enterPinViewController = self.storyboard.instantiateViewController(identifier: "EnterPinViewController") as! EnterPinViewController
        self.navigationController.present(enterPinViewController, animated: true, completion: nil)

    } else if UserDefaults.standard.bool(forKey: "LoggedIn") == true {

        let story = UIStoryboard(name: "Main", bundle:nil)
        let vc = story.instantiateViewController(withIdentifier: "MainController") as! UINavigationController
        UIApplication.shared.windows.first?.rootViewController = vc
        UIApplication.shared.windows.first?.makeKeyAndVisible()

        let mainViewController = self.storyboard.instantiateViewController(identifier: "ViewController") as! ViewController
        self.navigationController.present(mainViewController, animated: true, completion: nil)


    }

目前我将 UserDefaults 值设置为 true 和 false,并将 Pin 保存在 UserDefaults 本身中。我已经尝试更改 rootViewController 但没有任何反应意味着每次出现主屏幕而不是 Pin Screen。

let viewController = MainViewController()

guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
   return
 }
    
guard let window = appDelegate.window else {
   return
}
    
window.rootViewController = viewController

首先,您需要从 SceneDelegate 获取 Window 并将 window 用于 rootViewController.

建议:每当我们使用rootViewController时,就不需要做present推送 它会自动设置为主主页viewcontroller(初始viewController)

 let window = (UIApplication.shared.connectedScenes.first!.delegate as! SceneDelegate).window
            let storyboard = UIStoryboard(name: "Main", bundle:nil)
        if UserDefaults.standard.bool(forKey: "LoggedIn") == false {
            let vc =storyboard.instantiateViewController(withIdentifier: "EnterPin") as! UINavigationController
            window?.rootViewController = vc
            window?.makeKeyAndVisible()
        } else if UserDefaults.standard.bool(forKey: "LoggedIn") == true {
            let vc =storyboard.instantiateViewController(withIdentifier: "MainController") as! UINavigationController
            window?.rootViewController = vc
            window?.makeKeyAndVisible()
        }

建议你在 SceneDelegate 文件中做所有这些事情

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        guard let scene = (scene as? UIWindowScene) else { return }
        self.window = UIWindow(windowScene: scene)
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        if UserDefaults.standard.bool(forKey: "LoggedIn") == false {
            let vc = storyboard.instantiateViewController(withIdentifier: "EnterPin") as! UINavigationController
            self.window?.rootViewController = vc
            self.window?.makeKeyAndVisible()
        } else if UserDefaults.standard.bool(forKey: "LoggedIn") == true {
            let vc = storyboard.instantiateViewController(withIdentifier: "MainController") as! UINavigationController
            self.window?.rootViewController = vc
            self.window?.makeKeyAndVisible()
        }
    }

希望对您有所帮助!