I have created a simple List with a horizontal stack view (label, button, button). each button has his own button action but when I run I can see tap on one button print two actions. breakpoint also comes inside both actions. her is my code
var body: some View {
NavigationView {
List {
ForEach(self.heroViewModel.heros, id: \.self) { hero in
Section(header: Text(hero.name)) {
ForEach(hero.movies, id: \.self) { movieName in
HStack {
Text(movieName)
.onTapGesture {
return
}.frame(width: 150, height: 30, alignment: .leading)
Spacer()
Button(action: {
print("Rate us")
}, label: {
Text("Rate us")
.background(Color.red)
}).padding()
Spacer()
Button(action: {
print("watch me")
}, label: {
Text("Watch")
.background(Color.red)
}).padding()
}
}
}
}
}.navigationBarTitle("Heros List")
}
}
Need to use onTapGesture
instead of action
like this way.
Button(action: {}) {
Text("watch")
}
.frame(minWidth: 0, maxWidth: .infinity)
.background(Color.red)
.onTapGesture {
print("watch")
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With