在首次运行应用程序后尝试更改根视图控制器,但它崩溃了。
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)的未捕获异常终止
谢谢。
答案 0 :(得分:0)
您不能直接通过viewController名称设置rootViewController。请检查以下代码
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController = storyboard.instantiateViewController(withIdentifier: "LoginSignupVC")
self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()