gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 15:52:31 27 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





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的图像

enter image description here

最佳答案

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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com