-1

Trying to change the root view controller after the first time running the app but it crashes.

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        if(UserDefaults.standard.bool(forKey: "notFirstInApp") == false){
            UserDefaults.standard.set(true, forKey: "notFirstInApp")
            let state = StateTableViewController()
            window?.rootViewController = state
        }else{
            let home = HomeViewController()
            window?.rootViewController = home
        }

        return true
    }

Crashes with

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

Thanks in advance.

Yuchi
  • 3
  • 2
  • This is not the right way of initializing ViewController. Please refer to this answer for details https://stackoverflow.com/a/30592893/5949670 – Roohul Jul 19 '19 at 03:25

1 Answers1

0

You can not directly set rootViewController by viewController name. Please check below code

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController = storyboard.instantiateViewController(withIdentifier: "LoginSignupVC")
self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()
Nihar Dodiya
  • 135
  • 1
  • 9