[英]App Delegate crashes with NSException when trying to change root view controller
Trying to change the root view controller after the first time running the app but it crashes.在第一次运行应用程序后尝试更改根视图控制器但它崩溃了。
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
}
Crashes with崩溃与
libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)
libc++abi.dylib:以未捕获的 NSException 类型异常(lldb)终止
Thanks in advance.提前致谢。
You can not directly set rootViewController by viewController name.您不能通过 viewController 名称直接设置 rootViewController。 Please check below code
请检查以下代码
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController = storyboard.instantiateViewController(withIdentifier: "LoginSignupVC")
self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.