In SwiftUI I have created a loop of button.
I have to get tag value when button was tapped.
I have tried many times. I cant able to get which button was tapped
HStack(spacing: 1) {
ForEach(0...2, id: \.self) { j in
Button(action: {
// need button Tag
print("Tapped Button Tag:")
}, label: {
Text("")
})
.tag("\(j)")
}
}
It is not needed in tag, it is possible to do this directly, like
HStack(spacing: 1) {
ForEach(0...2, id: \.self) { j in
Button(action: {
// j is available here from context
print("Tapped Button Tag: \(j)") // << here !!
}, label: {
Text("")
})
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.