Swift - 在 AppDelegate 中实例化视图控制器

Swift - Instantiate View Controller in AppDelegate

我正在尝试让我的登录页面在 AppDelegate.swift 的初始视图控制器上弹出,我正在使用
let authVC = storybaord.instantiateViewController(withIdentifier: "StartVC")
实例化它。但是,视图控制器链接到导航控制器,当我使用代码时,当我单击视图控制器上的按钮时,它会使应用程序崩溃。代码:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    FirebaseApp.configure()

    if Auth.auth().currentUser == nil {
        let storybaord = UIStoryboard(name: "Main", bundle: Bundle.main)
        let authVC = storybaord.instantiateViewController(withIdentifier: "StartVC")
        window?.makeKeyAndVisible()
        window?.rootViewController?.present(authVC, animated: false, completion: nil)
    }
    return true
}

Main.Storyboard 文件中,我在导航控制器的第一个视图控制器上有故事板 ID ("StartVC")。截图:http://prntscr.com/lp8fez | http://prntscr.com/lp8flq

这是崩溃日志: Terminating app due to uncaught exception 'NSGenericException', reason: 'Could not find a navigation controller for segue 'toName'. Push segues can only be used when the source controller is managed by an instance of UINavigationController.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
if Auth.auth().currentUser == nil {
    let storybaord = UIStoryboard(name: "Main", bundle: Bundle.main)
    let authVC = storybaord.instantiateViewController(withIdentifier: "StartVC")


    window?.rootViewController?= authVC

                 window?.makeKeyAndVisible()
}
return true
}

我用过这段代码,它对我有用。如果需要更多帮助,请尝试告诉我。