简体   繁体   中英

Swift - Instantiate View Controller in AppDelegate

I am trying to have my login page pop up over the initial view controller from the AppDelegate.swift and I am using
let authVC = storybaord.instantiateViewController(withIdentifier: "StartVC")
to instantiate it. However, the View Controller is linked to a navigation controller which when I use the code it crashes the app when I click a button the the View Controller. Code:

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
}

In the Main.Storyboard file, I have the storyboard ID ("StartVC") on the first view controller of the Navigation Controller. ScreenShots : http://prntscr.com/lp8fez | http://prntscr.com/lp8flq

Heres the crash Log: 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. 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
}

I've used this code and it's working for me . Try and let me know if more help needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM