美文网首页
SwiftUI 之 Environment

SwiftUI 之 Environment

作者: 小凡凡520 | 来源:发表于2019-10-29 14:40 被阅读0次
final class Person:ObservableObject {
    
    var name = "chen"
}

struct PageView: View {
    
    @EnvironmentObject var person:Person
    
    var body: some View {
        Text("\(self.person.name)")
    }
}

struct PageView_Preview: PreviewProvider {
    
    static var previews: some View {
        PageView()
    }
}







func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
    // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
    // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).

    // Create the SwiftUI view that provides the window contents.
    let contentView = PageView().environmentObject(Person())

    // Use a UIHostingController as window root view controller.
    if let windowScene = scene as? UIWindowScene {
        let window = UIWindow(windowScene: windowScene)
        window.rootViewController = UIHostingController(rootView: contentView)
        self.window = window
        window.makeKeyAndVisible()
    }
}

相关文章

网友评论

      本文标题:SwiftUI 之 Environment

      本文链接:https://www.haomeiwen.com/subject/rnkwmctx.html