This question already has an answer here:
Xcode 11 & iOS13, using UIKIT can't change background colour of UIViewController

(1 个回答)


1年前关闭。




我正在尝试以编程方式设置初始 View Controller ,但我不断收到此错误。任何解决方案?
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?

这是我写的代码。
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
    }
}


这是我的 Storyboard的图像

ios - 在 Xcode 11.2 中使用 Storyboards 以编程方式设置初始 View  Controller-LMLPHP

最佳答案

iOS 13 已将 windows 设置从 AppDelegate 移至 SceneDelegate 以支持使用(可能是多个)场景而不是单个窗口。您现在必须像这样进行设置:

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()
   }

关于ios - 在 Xcode 11.2 中使用 Storyboards 以编程方式设置初始 View Controller ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58748195/

10-11 15:42