SwiftUI EnvironmentObject在视图初始化程序中不可用?

时间:2020-03-03 19:46:45

标签: swiftui

我成功地将environmentObject appSettings传递到了视图中。我可以使用它来修改视图中的字体和选择器。但是,如果我尝试在视图init()中访问environmentObject发布的变量,则会崩溃:

Thread 1: Fatal error: No ObservableObject of type AppSettings found.
A View.environmentObject(_:) for AppSettings may be missing as an ancestor of this view.

在自定义SwiftUI View初始化程序中使用environmentObject是否有特殊规则?

这是我的视图代码的开始。 environmentObject是appSettings。如果我在初始值设定项中注释掉了第2行,而取消注释了第3行,则该应用程序可以工作。请注意,稍后我会在选择器中成功使用“ appSettings.interfaces”。

struct CaptureFilterView: View {

@State var etherCapture: EtherCapture? = nil
@EnvironmentObject var appSettings: AppSettings
@Binding var frames: [Frame]
@State var captureFilter: String = ""
@State var error: String = ""
@State var numberPackets = 10

@State var interface: String = ""
init(frames: Binding<[Frame]>) {
    self._frames = frames
    self.interface = appSettings.interfaces.first ?? "en0" //CRASH HERE
    //self.interface = "en0"  //uncomment this and comment line above to make app "work"
}
var body: some View {
    HStack() {
        ...
        Picker(selection: $interface, label: Text("")) {
            ForEach(appSettings.interfaces, id: \.self) { interfaceName in
                Text(interfaceName).tag(interfaceName)
            }
        }

在这里我可以在AppDelegate.swift中创建顶级内容视图

        let contentView = ContentView(showCapture: true).environmentObject(appSettings)

只是为了确保,我在顶层ContentView中创建CaptureFilterView时也传递了environmentObject。这不是必需的,并且不会改变行为。

            if showCapture { CaptureFilterView(frames: self.$frames).environmentObject(appSettings) }

作为参考,这是我的appSettings的顶部:

class AppSettings: ObservableObject {
    @Published var font: Font
    @Published var interfaces: [String]

1 个答案:

答案 0 :(得分:4)

SwiftUI EnvironmentObject在视图初始化程序中不可用?

是的,SwiftUI EnvironmentObject在View初始化程序中不可用。为什么?这很简单-在对象初始化后注入

让我们考虑如何完成上述ContentView的示例:

let contentView = ContentView(showCapture: true).environmentObject(appSettings)

那么这是怎么回事?在这里

1)实例化和初始化ContentView类型的值

let newInstance = ContentView.init(showCapture: true) 

2)在注入func environmentObject()属性的newInstance上调用函数appSetting

let contentView = newInstance.environmentObject(appSettings)