设置rootViewController iOS 13 [英] Set rootViewController iOS 13

查看:399
本文介绍了设置rootViewController iOS 13的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

升级Xcode后,我的应用程序的关键部分已停止工作.

After upgrading Xcode a critical part of my application has stopped working.

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

When my app launches I run a function to check boolean flags and set the correct rootViewController.

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

But the code I have been using to set this has now stopped working

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()
}

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

Specifically when the app gets the the second last line window.rootViewController = controller it crashes with a libc++abi.dylib: terminating with uncaught exception of type NSException error.

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

The above function is in a class called Utilities.swift and I am calling the function from within my AppDelegate.swift as shown below:

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
    }

非常感谢我设置根控制器的任何解决方案或修复方法.

Any solutions or fixes on how I can set the root controller is much appreciated.

谢谢!

推荐答案

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

This is because AppDelegate doesn't have window property anymore. Now you must use SceneDelegate's scene(_:willConnectTo:options:) method to change root view controller. Like shown in this example:

    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()
    }

这篇关于设置rootViewController iOS 13的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆