请参阅以下TestView。当我单击“按钮5”时,我看到警报显示“ 单击按钮0 ”。
struct TestView: View {
@State var showAlert = false
var body: some View {
VStack {
ForEach(0...5,id: \.self) { n in
Button(action: {
self.showAlert.toggle()
}) {
Text("Button \(n)")
}
.padding(20)
.border(Color.green,width: 4)
.padding(8)
.alert(isPresented: self.$showAlert) {
Alert(title: Text("Button \(n) clicked"))
}
}
}
}
}
无论我单击哪个按钮,警报始终显示“单击了按钮0” 。我期望每个按钮应该显示自己的按钮索引。想知道为什么吗?