我通常使用情节提要通过单击属性检查器将视图控制器设置为初始视图控制器。
如何在 swift UI 中设置初始视图控制器?
系统信息: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()
}
}
Run Code Online (Sandbox Code Playgroud)
将行更改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()
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3094 次 |
最近记录: |