gpt4 book ai didi

ios - HStack 中的两个按钮相互执行操作

转载 作者:行者123 更新时间:2023-12-02 00:03:59 26 4
gpt4 key购买 nike

我创建了一个带有水平堆栈 View (标签、按钮、按钮)的简单列表。每个按钮都有自己的按钮操作,但是当我运行时,我可以看到点击一个按钮会打印两个操作。断点也出现在这两个 Action 中。她是我的代码gif

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

Button(action: {}) {
Text("watch")
}
.frame(minWidth: 0, maxWidth: .infinity)
.background(Color.red)
.onTapGesture {
print("watch")
}

关于ios - HStack 中的两个按钮相互执行操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58514891/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com