【发布时间】:2020-11-14 09:10:51
【问题描述】:
我在 swiftUI 视图中有一个状态变量 @State private var value: Int。当我在像 value == 10 这样的布尔语句中使用该状态时,如果传递给第二个视图,它不会更新。
例如,下面的高亮显示永远不会改变:
struct TestView: View {
@State private var value = 5
var body: some View {
Button("update value") {
withAnimation { value = value + 5 }
}
Text("\(value)").padding()
MyTestView(highlighted: value == 5).padding()
MyTestView(highlighted: value == 10).padding()
MyTestView(highlighted: value == 15).padding()
}
}
struct MyTestView: View {
@State var highlighted: Bool = false
var body: some View {
Text("test")
.background(highlighted ? Color.red : Color.white)
}
}
【问题讨论】:
-
您对问题的第二次编辑与白天和黑夜不同!你问了一个多么确切的问题!