SwiftUI 环境对象未更新
SwiftUI Environment Object Not Getting Updated
我认为指定为环境对象的 class 中的 @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
}
我发现我的连接方式 @EnvironmentObject
实际上工作得很好,我在另一个区域的代码中有一个错误阻止视图接收我正在寻找的数据,这让我想到这与 @EnvironmentObject
无法正常工作有关。
我认为指定为环境对象的 class 中的 @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
}
我发现我的连接方式 @EnvironmentObject
实际上工作得很好,我在另一个区域的代码中有一个错误阻止视图接收我正在寻找的数据,这让我想到这与 @EnvironmentObject
无法正常工作有关。