当我更新isDisabled
视图中的状态变量时,它会.disabled
按预期更新我的文本字段的修饰符,但随后会导致控制台中出现大约 40 个以下错误实例(最后属性编号不同):
=== AttributeGraph: cycle detected through attribute 200472 ===
然后它说:AttributeGraphError[59460:4808136] [SwiftUI] Modifying state during view update, this will cause undefined behavior.
这是产生错误的代码的最小版本:
struct ContentView: View {
@State var isDisabled = false
@State var text = ""
var body: some View {
VStack {
TextField("", text: $text)
.textFieldStyle(.roundedBorder)
.disabled(isDisabled)
Button("Disable text field") { isDisabled = true }
}
}
}
如何修复此错误?