.environmentObject() 视图运算符 vs @EnvironmentObject 的目的是什么? [英] What's the purpose of .environmentObject() view operator vs @EnvironmentObject?

查看:28
本文介绍了.environmentObject() 视图运算符 vs @EnvironmentObject 的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图从这里众所周知的新手深渊中爬出来.

我开始掌握 @EnvironmentObject 的使用,直到我注意到文档中的 .environmentObject() 视图运算符.

这是我的代码:

I'm attempting to crawl out of the proverbial Neophyte abyss here.

I'm beginning to grasp the use of @EnvironmentObject till I notice the .environmentObject() view operator in the docs.

Here's my code:

import SwiftUI

struct SecondarySwiftUI: View {
    @EnvironmentObject var settings: Settings

    var body: some View {
        ZStack {
            Color.red
            Text("Chosen One: \(settings.pickerSelection.name)")
        }.environmentObject(settings) //...doesn't appear to be of use.
    }

    func doSomething() {}
}

我试图用视图上的 .environmentObject() 运算符替换 @EnvironmentObject 的使用.
我因缺少设置"定义而出现编译错误.

但是,代码在没有 .environmentObject 运算符的情况下可以正常运行.

所以我的问题是,为什么有 .environmentObject 运算符?

.environmentObject() 是实例化环境对象还是 @environmentObject 访问实例化的对象?

I tried to replace the use of the @EnvironmentObject with the .environmentObject() operator on the view.
I got a compile error for missing 'settings' def.

However, the code runs okay without the .environmentObject operator.

So my question, why have the .environmentObject operator?

Does the .environmentObject() instantiates an environmentObject versus the @environmentObject accesses the instantiated object?

推荐答案

这里是演示代码,用于展示 EnvironmentObject 的变体 &.environmentObject 用法(内联注释):

Here is demo code to show variants of EnvironmentObject & .environmentObject usage (with comments inline):

struct RootView_Previews: PreviewProvider {
    static var previews: some View {
        RootView().environmentObject(Settings()) // environment object injection
    }
}

class Settings: ObservableObject {
    @Published var foo = "Foo"
}

struct RootView: View {
    @EnvironmentObject var settings: Settings // declaration for request of environment object

    @State private var showingSheet = false
    var body: some View {
        VStack {
            View1() // environment object injected implicitly as it is subview
            .sheet(isPresented: $showingSheet) {
                View2() // sheet is different view hierarchy, so explicit injection below is a must
                    .environmentObject(self.settings) // !! comment this out and see run-time exception
            }
            Divider()
            Button("Show View2") {
                self.showingSheet.toggle()
            }
        }
    }
}

struct View1: View {
    @EnvironmentObject var settings: Settings // declaration for request of environment object
    var body: some View {
        Text("View1: \(settings.foo)")
    }
}

struct View2: View {
    @EnvironmentObject var settings: Settings // declaration for request of environment object
    var body: some View {
        Text("View2: \(settings.foo)")
    }
}

所以,在你的代码中 ZStack 没有声明环境对象的需要,所以没有使用 .environmentObject 修饰符.

So, in your code ZStack does not declare needs of environment object, so no use of .environmentObject modifier.

这篇关于.environmentObject() 视图运算符 vs @EnvironmentObject 的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆