Search code examples
swiftmodel-view-controlleruinavigationcontrollerappdelegaterootviewcontroller

Set NavigationBarController to rootViewController


I try to use MVC pattern, where my NavigationController instantiates and controls all my views and the corresponding view switches. To achieve this, I just instantiate my navigationController and set it as rootViewController in the app delegate.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    self.window = UIWindow(frame: UIScreen.main.bounds)
    let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let navigationController: NavigationController = (mainStoryboard.instantiateViewController(withIdentifier: "navigationController") as! NavigationController)

    self.window?.rootViewController = navigationController
    self.window?.makeKeyAndVisible()
    return true
}

In the viewDidLoad() function of my NavigationController class, I just instantiate my first "real" view with myTableViewController and push it in front.

override func viewDidLoad() {
    super.viewDidLoad()
    let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    self.myTableViewController = mainStoryboard.instantiateViewController(withIdentifier: "myTableViewController") as? MyTableViewController
    self.pushViewController(myTableViewController!, animated: true)
}

Although everything works fine, I get the console warning:

Failed to instantiate the default view controller for UIMainStoryboardFile 'Main' - perhaps the designated entry point is not set?

I think this warning occurs, because the navigationController is not a "real" ViewController... But how can I fix it without instantiate MyTableView() in App delegate?


Solution

  • enter image description here

    Please check your Storyboard set as Initial View Controller.