我以为是这样的情况,指定为环境对象的类中的@Published属性会自动更新为我将环境对象传递给的所有子视图,但是下面的代码没有更新,我在做什么错?
class trackerDataStore: ObservableObject {
let healthStore = HKHealthStore()
@Published var isLoadingWorkouts = false
@Published var subscriptionIsActive = false
@Published var trackerWorkoutObject: trackerWorkoutObject?
}
struct detailHeaderCard: View {
@EnvironmentObject var trackerDataStore: TrackerDataStore
var body: some View {
//omitted
}
.sheet(isPresented: $isShowingPlayerAddStatsFormAsModal) {
PlayerAddStatsForm(isShowingPlayerAddStatsFormAsModal: $isShowingPlayerAddStatsFormAsModal)
.environmentObject(trackerDataStore)
}
}
struct PlayerAddStatsForm: View {
@EnvironmentObject var trackerDataStore: TrackerDataStore
//not getting reactively updated here
}
答案 0 :(得分:0)
我发现连接@EnvironmentObject
的方式实际上工作良好,我在另一个区域的代码中有一个错误,该错误阻止了视图接收我正在寻找的数据,这使我认为与@EnvironmentObject
有关,无法正常工作。