简体   繁体   English

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

[英]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.

相关问题 IOS | 无法在应用委托中更改根视图 controller - IOS | Unable to change root view controller in app delegate 在app委托中弹出到根视图控制器 - Pop to root view controller in app delegate 尝试从Web视图委托推送新的视图控制器时,应用程序崩溃 - App crashes while trying to push a new view controller from web view delegate UIImagePickerController的委托不是视图控制器时会崩溃 - UIImagePickerController crashes when its delegate is not the view controller iOS更改视图控制器来自应用程序委托? - iOS change view controller from the app delegate? 标签栏不是根视图控制器时,无法更改App Delegate中的UITabBarController徽章 - Can't alter UITabBarController badge in App Delegate when Tab Bar isn't root view controller 将UITabBarController设置为根视图控制器时,如何修复应用程序委托中的“意外找到nil” - How to fix “Unexpectedly found nil” in app Delegate when setting UITabBarController as root view controller 尝试更改UITabBar图标时收到NSException - Receiving NSException when trying to change UITabBar icon 服务器调用App Delegate后如何更改根控制器? - How to change root controller after a server call on App Delegate? iOS(Swift)-投射应用程序委托窗口的根视图控制器可在模拟器中运行,但在手机上加载应用程序时失败 - iOS (Swift) - Casting root view controller of the app delegate's window works in simulator, but fails when app is loaded on phone
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM