我有一个父视图,其中有两个子视图。
当子视图更改首选项值时,父视图通过onPreferenceChange
接收首选项更改
但是,当父母有日期选择器时,onPreferenceChange
不会被调用并停止工作。
有人找到任何解决方法吗?
struct parentView: View {
@State private var date = Date()
var body: some View {
VStack{
ChildView1()
Text("Child View 2")
// DatePicker("", selection: self.$date, displayedComponents: .hourAndMinute)
}
.onPreferenceChange(testPreference.self, perform: { value in
print("Printing preference value")
print(value)
})
}
}
struct ChildView1: View {
@State private var tablets : Int = 0
var body: some View {
Text("Child View 1")
.onTapGesture {
self.tablets = self.tablets + 1
}
.preference(key: testPreference.self, value: self.tablets)
}
}
struct testPreference: PreferenceKey {
typealias Value = Int
static var defaultValue: Int = 0
static func reduce(value: inout Int, nextValue: () -> Int) {
value = nextValue()
}
}