HStack 中的两个 Button 互相作用 [英] Two Buttons inside HStack taking action of each other
本文介绍了HStack 中的两个 Button 互相作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我创建了一个带有水平堆栈视图(标签、按钮、按钮)的简单列表.每个按钮都有自己的按钮动作,但是当我运行时,我可以看到点击一个按钮会打印两个动作.断点也出现在这两个操作中.她是我的代码
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")
}
}
推荐答案
需要像这样使用 onTapGesture
而不是 action
.
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")
}
这篇关于HStack 中的两个 Button 互相作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文