Add EnvironmentObject in SwiftUI 2.0

拈花ヽ惹草 提交于 2020-12-15 06:05:33

问题


Since SwiftUI 2.0 does not have an AppDelegate and SceneDelegate anymore, where should EnvironmentObjects be set?

This how it was done previously, where do i have to add them now?

window.rootViewController = UIHostingController(rootView: ContentView()
                             .environmentObject(settings))

回答1:


Try the following:

@main
struct TestApp: App {
    @StateObject var settings: Settings = ... // init here

    var body: some Scene {
        WindowGroup {
            ContentView()
                .environmentObject(settings)
        }
    }
}


来源:https://stackoverflow.com/questions/63172717/add-environmentobject-in-swiftui-2-0

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!