0

以下代码产生运行时错误:@EnvironmentObject 错误:可能缺少此视图的祖先。环境中的 tState 是一个@ObservedObject。

struct TEditorView: View {
    @EnvironmentObject private var tState: TState
    
    @State var name = ""
    
    init() {
        self._name = State(initialValue: tState.name)
    }
 
    var body: some View {
        ...
    }
}

XCode 12.0.1 iOS 14

4

3 回答 3

1

答案是环境对象显然无法在 init() 函数中访问。但是,ObservedObject 可以。所以我把代码改成了这个,它可以工作。为了方便起见,我将 TState 变成了一个可以在任何地方访问的单例。在许多情况下,这可能会取代 @EnvironmentObject 的使用。

struct TEditorView: View {
    @ObservedObject private var tState = TState.shared
    //@EnvironmentObject private var tState: TState
    
    @State var name = ""
    
    init() {
        self._name = State(initialValue: tState.name)
    }
 
    var body: some View {
        ...
    }
}

于 2020-10-22T00:40:12.650 回答
0

这里的另一种方法可能是TState在构造函数中注入初始值并完全取消@EnvironmentObject。然后从父视图中,您可以@EnvironmentObject在创建视图时使用该值。

struct TEditorView: View {
    @State var name = ""
    
    init(tState: TState) {
        self._name = State(initialValue: tState.name)
    }
 
    var body: some View {
        ...
    }
}

struct ContentView: View {
    @EnvironmentObject private var tState: TState
    
    var body: some View {
        TEditorView(state: tState)
    }
}

或者,如果该值是双向的,则使用 a@Binding而不是。@Statename

一般来说,我也会质疑为什么你需要@EnvironmentObject在构造函数中。这个想法是@EnvironmentObject,它在所有视图中都表示相同,所以你应该只需要它body

如果您需要任何数据转换,则应在对象模型本身而不是视图中完成。

于 2020-10-22T00:47:47.743 回答
0

@State应该根据文档进行设置,只能privateView body.

https://developer.apple.com/documentation/swiftui/state

@EnvironmentObject应使用YourObservableObjectContentView().environmentObject(设置)

https://developer.apple.com/documentation/combine/observableobject https://developer.apple.com/documentation/swiftui/stateobject

下面是一些示例代码

import SwiftUI
class SampleOO: ObservableObject {
    @Published var name: String = "init name"
}
//ParentView
struct OOSample: View {
    //The first version of an @EnvironmentObject is an @ObservedObject or @StateObject
    //https://developer.apple.com/tutorials/swiftui/handling-user-input
    @ObservedObject var sampleOO: SampleOO = SampleOO()
    var body: some View {
        VStack{
            Button("change-name", action: {
                self.sampleOO.name = "OOSample"
            })
        
        Text("OOSample = " + sampleOO.name)
        //Doing this should fix your error code with no other workarounds
        ChildEO().environmentObject(sampleOO)
        SimpleChild(name: sampleOO.name)
        }
    }
}
//Can Display and Change name
struct ChildEO: View {
    @EnvironmentObject var sampleOO: SampleOO
    var body: some View {
        VStack{
            //Can change name
            Button("ChildEO change-name", action: {
                self.sampleOO.name = "ChildEO"
            })
            Text("ChildEO = " + sampleOO.name)
        }
    }
}
//Can only display name
struct SimpleChild: View {
    var name: String
    var body: some View {
        VStack{
            //Cannot change name
            Button("SimpleChild - change-name", action: {
                print("Can't change name")
                //self.name = "SimpleChild"
            })
            Text("SimpleChild = " + name)
        }
    }
}

struct OOSample_Previews: PreviewProvider {
    static var previews: some View {
        OOSample()
    }
}
于 2020-10-22T12:36:00.153 回答