Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically set the initial view controller using Storyboards in Xcode 11.2 [duplicate]

Tags:

xcode

ios

swift

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

like image 872
M B Avatar asked Dec 01 '22 13:12

M B


2 Answers

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()
   }
like image 129
flanker Avatar answered Dec 11 '22 00:12

flanker


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

like image 21
Adarsh KC Avatar answered Dec 11 '22 00:12

Adarsh KC