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

邮差的信 提交于 2019-12-12 04:28:18

问题


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)


回答1:


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




回答2:


In the Storyboard select the LoginVC and in Inspector window give loginview identifier in the Storyboard ID



来源:https://stackoverflow.com/questions/41711734/appdelegate-didfinishlaunchingwithoptions-launchoptions-terminating-with-uncau

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!