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

时间:2018-12-01 03:59:09

标签: ios swift storyboard

我正在尝试从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文件中,我在Navigation Controller的第一个视图控制器上具有情节提要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.

1 个答案:

答案 0 :(得分:1)

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
}

我已经使用了此代码,并且对我有用。尝试让我知道是否需要更多帮助。