下面的代码创建了一个简单的代码HStack
,最终看起来像这样:
问题是点击“增量”会增加“计数”而不是“嵌套”。有谁知道为什么会这样,以及如何解决这个问题?State
或者当 SwiftUI 视图嵌套在变量中时,它们是否会从根本上中断?
struct ContentView: View {
var body: some View {
VStack {
Text("Count: \(count)")
nested
Button(action: {
self.count += 1
self.nested.count += 1
}) { Text("Increment") }
}
}
@State var count = 0
struct Nested: View {
var body: some View {
Text("Nested: \(count)")
}
@State var count = 0
}
@State var nested = Nested()
}