AppDelegate didFinishLaunchingWithOptions launchOp

2019-09-07 15:48发布

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)

2条回答
▲ chillily
2楼-- · 2019-09-07 15:56

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

查看更多
女痞
3楼-- · 2019-09-07 16:18

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

enter image description here

查看更多
登录 后发表回答