6

I am trying to programmatically set the initial View controller but i keep getting this Error. Any solutions?

2019-11-07 11:47:43.975990+0000 RestaurantApp[16319:147412] [WindowScene] Failed to instantiate the default view controller for UIMainStoryboardFile 'Main' - perhaps the designated entry point is not set?

Here is the code that i have Written.

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {


    let window = UIWindow()
    let locationService = LocationService()
    let storyboard = UIStoryboard(name: "Main", bundle: nil) //refernce to our storyboard





    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.

        //setiing the root view control on our window
        switch locationService.status  {
        case .notDetermined, .denied, .restricted:
            let LocationViewController =
                storyboard.instantiateViewController (withIdentifier: "LocationViewController") as? LocationViewController
            LocationViewController?.locationService = locationService
                window.rootViewController = LocationViewController
        default:
            assertionFailure()
        }

        window.makeKeyAndVisible()

        return true
    }
}

Here is an Image of my storyboard

enter image description here

M B
  • 197
  • 2
  • 6
  • 18

4 Answers4

15

iOS 13 has moved the windows setup from AppDelegate to SceneDelegate to support the use of (possibly multiple) scenes rather than a single window. You now have to do the setup like this:

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

   var window: UIWindow?
   let storyboard = UIStoryboard(name: "Main", bundle: nil)

   func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
      guard let windowScene = scene as? UIWindowScene else { return }
      let vc = storyboard.instantiateViewController (withIdentifier: "Primary") as! ViewController
      window = UIWindow(windowScene: windowScene)
      window?.rootViewController = vc
      window?.makeKeyAndVisible()
   }
ThomasW
  • 16,981
  • 4
  • 79
  • 106
flanker
  • 3,840
  • 1
  • 12
  • 20
  • I want to change rootViewController from my another view controller. So, Could you please suggest code? i.e. logout from app and show login screen or login and set home screen with left menu as root view controller. – Sagar Chauhan Nov 13 '19 at 19:18
  • That's a separate topic, so I don't want to get into it in the comments on this one. This may help as it has similar needs https://stackoverflow.com/questions/58845347/performing-a-completion-handler-before-app-launches or raise another question. – flanker Nov 13 '19 at 21:42
4
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
            let storyboard = UIStoryboard.init(name: "Main", bundle: nil)
     let homeView =  storyboard.instantiateViewController(withIdentifier: "ViewController") as! ViewController
     self.window?.rootViewController = homeView
    return true
}

this works for me

Adarsh KC
  • 449
  • 3
  • 18
1

This error happens due to a simple mistake in your storyboard, and it’s easy to fix. When your app starts, iOS needs to know precisely which view controller needs to be shown first – known as your default view controller.

If you accidentally deleted that view controller, or otherwise made it not the default, then you’ll see the error “Failed to instantiate the default view controller for UIMainStoryboardFile 'Main' - perhaps the designated entry point is not set?” when your app launches, along with a plain black screen.

To fix the problem, open your Main.storyboard file and find whichever view controller you want to be shown when your app first runs. When it’s selected, go to the attributes inspector and check the box marked “Is Initial View Controller”. You should see a right-facing arrow appear to the left of that view controller, showing that it’s your storyboard’s entry point.

SGDev
  • 2,256
  • 2
  • 13
  • 29
0

Go back to storyboard and checkmark this to make the viewController you want your app to start off to,

Checkmark this

Frostmourne
  • 156
  • 1
  • 19