尝试更改根视图控制器时,App Delegate会因NSException崩溃

时间:2019-07-19 02:32:28

标签: ios swift appdelegate

在首次运行应用程序后尝试更改根视图控制器,但它崩溃了。

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        if(UserDefaults.standard.bool(forKey: "notFirstInApp") == false){
            UserDefaults.standard.set(true, forKey: "notFirstInApp")
            let state = StateTableViewController()
            window?.rootViewController = state
        }else{
            let home = HomeViewController()
            window?.rootViewController = home
        }

        return true
    }

崩溃

  

libc ++ abi.dylib:以类型为NSException(lldb)的未捕获异常终止

谢谢。

1 个答案:

答案 0 :(得分:0)

您不能直接通过viewController名称设置rootViewController。请检查以下代码

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController = storyboard.instantiateViewController(withIdentifier: "LoginSignupVC")
self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()