【发布时间】:2021-03-19 05:35:58
【问题描述】:
我在我的 SwiftUI ContentView 中有关注 Picker:
Picker("", selection: $approxType, content: {
Text("Linear").tag(1)
Text("Quadratic").tag(2)
Text("Exponential").tag(3)
})
.pickerStyle(SegmentedPickerStyle())
.frame(width: 300, alignment: .leading)
.onChange(of: approxType, perform: { value in
calculated = false
})
我想知道何时执行onChange 修饰符。好吧,似乎 ContentView 先刷新,然后才执行 onChange 闭包。我需要确保在刷新 ContentView 之前将 @State var calculated 设置为 false。我很感激任何提示。
【问题讨论】:
-
更改 Picker 选择不应强制刷新整个 ContentView。我猜想在您看来,“计算”与其他事物的关联方式存在问题?如果你单独测试这段代码,它会正确执行。
-
你不应该依赖改变状态的顺序 - 只对它们做出反应。
-
我不认为我完全理解这个问题,但也许你正在寻找
.onAppear