ios - 尝试更改根视图控制器时,App Delegate 崩溃并出现 NSException
问题描述
第一次运行应用程序后尝试更改根视图控制器,但它崩溃了。
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) 类型的未捕获异常终止
提前致谢。
解决方案
您不能通过 viewController 名称直接设置 rootViewController。请检查以下代码
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController = storyboard.instantiateViewController(withIdentifier: "LoginSignupVC")
self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()
推荐阅读
- javascript - 如何让不和谐的机器人提及用户?
- linux - 有什么方法可以确保向 Linux 中的某个站点发送的 HTTP 请求不超过 x 个?
- java - 如何根据用户角色控制 Java android 应用程序中的组件可见性?
- java - 如何测试依赖时间的聚合?
- yii2 - Yii2 DynaGrid - 如何从导出菜单中隐藏格式
- python - BeautifulSoup 无法从 HKJC 网站上抓取数据
- java - 为什么 JpaRpository.delete(entity) 实际上并没有删除实体?
- sql - Azure 弹性作业删除
- spring-boot - Spring boot docker更新不出现
- ios - SwiftUI 中的 CoreData:如何清楚地获取属性?