gpt4 book ai didi

swift - 如何为 swiftUI 设置初始 View Controller

转载 作者:行者123 更新时间:2023-12-01 13:32:58 25 4
gpt4 key购买 nike

我通常使用 Storyboard 通过单击属性检查器将 View Controller 设置为初始 View Controller 。

如何在 swift UI 中设置初始 View Controller ?

系统信息:Swift 5,Xcode 11.3.1。

最佳答案

在 SceneDelegate.swift

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
let contentView = ContentView().environment(\.managedObjectContext, context)

if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
window.rootViewController = UIHostingController(rootView: contentView)
self.window = window
window.makeKeyAndVisible()
}
}

换行 let contentView = ContentView()...YourInitialView()...
结果应该是这样的
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// change this line to your initial view controller class name
let contentView = YourInitalView().environment(\.managedObjectContext, context)
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
window.rootViewController = UIHostingController(rootView: contentView)
self.window = window
window.makeKeyAndVisible()
}
}

关于swift - 如何为 swiftUI 设置初始 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60385867/

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