设置 rootViewController iOS 13

Oli*_*ris 9 uiwindow ios rootview swift ios13

升级 Xcode 后,我的应用程序的一个关键部分停止工作。

当我的应用程序启动时,我运行一个函数来检查布尔标志并设置正确的 rootViewController。

但是我用来设置它的代码现在已经停止工作了

class func setLoginAsInitialViewContoller(window:UIWindow) {
    print("SET LOGIN") 
    let storyboard = UIStoryboard(name: "Login", bundle: nil)
    let controller = storyboard.instantiateViewController(withIdentifier: "LoginViewController")
    controller.modalPresentationStyle = .overFullScreen
    window.rootViewController = controller
    window.makeKeyAndVisible()
}
Run Code Online (Sandbox Code Playgroud)

特别是当应用程序获得倒数第二行时,window.rootViewController = controller它会因libc++abi.dylib: terminating with uncaught exception of type NSException错误而崩溃。

上面的函数在一个被Utilities.swift调用的类中,我正在从我的内部调用该函数AppDelegate.swift,如下所示:

class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    var storyboard: UIStoryboard? = nil

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

        UIApplication.shared.isIdleTimerDisabled = true
        Utilities.decideInitialViewController(window: self.window!)

        return true
    }
Run Code Online (Sandbox Code Playgroud)

非常感谢有关如何设置根控制器的任何解决方案或修复程序。

谢谢!

lac*_*rin 14

这是因为 AppDelegate 不再有window属性。现在您必须使用 SceneDelegate 的scene(_:willConnectTo:options:)方法来更改根视图控制器。如本例所示:

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        guard let scene = (scene as? UIWindowScene) else { return }

        // Instantiate UIWindow with scene
        let window = UIWindow(windowScene: scene)
        // Assign window to SceneDelegate window property
        self.window = window
        // Set initial view controller from Main storyboard as root view controller of UIWindow
        self.window?.rootViewController = UIStoryboard(name: "Main", bundle: nil).instantiateInitialViewController()
        // Present window to screen
        self.window?.makeKeyAndVisible()
    }
Run Code Online (Sandbox Code Playgroud)


Nid*_*dhi 7

它在项目中的SceneDelegate.swift文件中可用

它将具有委托方法:

func 场景(_ 场景:UIScene,willConnectTo 会话:UISceneSession,选项 connectionOptions:UIScene.ConnectionOptions)

例子

func scene(_ scene: UIScene, willConnectTo session: UISceneSession,
 options connectionOptions: UIScene.ConnectionOptions) {


if let windowScene = scene as? UIWindowScene {

    self.window = UIWindow(windowScene: windowScene) 

    let initialViewController = 
        storyboard.instantiateViewController(withIdentifier: "FirstViewController")            
        self.window!.rootViewController = initialViewController
        self.window!.makeKeyAndVisible()
    }

}
Run Code Online (Sandbox Code Playgroud)


din*_*rma 5

viewDidAppear 中,您可以设置 root:-

  override func viewDidAppear(_ animated: Bool) {
            print(self.view.window)
            let vc = self.storyboard?.instantiateViewController(identifier: "SecondViewController") as? SecondViewController
            self.view.window?.rootViewController = vc
        }
Run Code Online (Sandbox Code Playgroud)