AppDelegate didFinishLaunchingWithOptions launchOptions - 以 NSException Swift 类型的未捕获异常终止 3.0

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

我正在尝试使用 Swift 3.0

创建 login/protected 页面会话页面

因此,我在 AppDelegate.swift 中创建了 didFinishLaunchingWithOptions launchOptions 函数,如下所示

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
    }
}

构建成功,但应用程序出现错误运行。报错如下

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)

在 Storyboard select LoginVC 和 Inspector window 中,在 Storyboard ID

中提供 loginview 标识符

很可能,您没有设置 LoginVC 的 Storyboard ID。 Select Storyboard 中的 LoginVC,并将 Storyboard ID 设置为 "loginview"。参考图片