简体   繁体   中英

AppDelegate didFinishLaunchingWithOptions launchOptions - terminating with uncaught exception of type NSException Swift 3.0

I'm trying to create login/protected page session page using Swift 3.0

Therefore, I created didFinishLaunchingWithOptions launchOptions function in AppDelegate.swift as below

AppDelegate.swift

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

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


        let rootViewController = self.window!.rootViewController
        let mainStoryBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)

        let isUserLoggedIn:Bool = UserDefaults.standard.bool(forKey: "isUserLoggedIn")
        if(!isUserLoggedIn){

            let loginViewController = mainStoryBoard.instantiateViewController(withIdentifier: "loginview") as! LoginVC

            window!.rootViewController = loginViewController
            window!.makeKeyAndVisible()

        }
        else{
            let protectedPage = mainStoryBoard.instantiateViewController(withIdentifier: "ViewController") as! ViewController

            window!.rootViewController = protectedPage
            window!.makeKeyAndVisible()
        }

        return true
    }
}

It build successfully, But i got an error when apps run. The error as below

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Storyboard () doesn't contain a view controller with identifier 'loginview''

libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)

在情节提要中选择LoginVC,然后在检查器窗口中的情节提要ID中提供loginview标识符

Most probably, you did not set the Storyboard ID of your LoginVC. Select the LoginVC in storyboard and set the storyboard ID as "loginview". See the image for reference

在此处输入图片说明

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