一尘不染

设置rootViewController iOS 13

swift

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

具体来说,当应用获得倒数第二行时,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
    }

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

谢谢!


阅读 591

收藏
2020-07-07

共1个答案

一尘不染

这是因为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()
    }
2020-07-07