[英]Strange buttons behaviour in a list (SwiftUI)
在下面的 SwiftUI 代碼中,我注意到了一些意外行為。
我想知道這是否是一個錯誤,這是否正常,或者我是否只是遺漏了一些明顯的東西。
List {
ForEach(self.myList, id: \.self.name) {
item in
HStack {
Spacer()
Button(action: {
print("Button One tapped!")
....
}) {
item.name.map(Text.init)
.font(.largeTitle)
.foregroundColor(.secondary)
}
Spacer()
Button(action: {
print("Button Two tapped!")
....
}) {
Image(systemName: "pencil.circle")
.font(.title)
.foregroundColor(.secondary)
.padding(.leading, 17)
}
}
}
.onDelete(perform: deleteFunc)
}
現在,當連續點擊兩個按鈕中的一個時會發生這種情況。 我可以看到這兩條消息:
Button One tapped!
Button Two tapped!
我希望只看到一條消息,具體取決於點擊的按鈕。
如果消息的順序根據點擊的按鈕而變化; 我可以使用一個或兩個 boolean 來執行我想要的最終結果。 但是這兩條消息總是以相同的順序出現。
有沒有人有同樣的經歷? 或者有人看到任何錯誤嗎?
使用PlainButtonStyle
(或任何自定義樣式),因為 List 會自動檢測默認按鈕樣式以突出顯示整行。
這是一個簡化的(來自您的代碼)演示:
struct DemoListWithButtons: View {
var body: some View {
List {
ForEach(0..<5, id: \.self) {
item in
HStack {
Spacer()
Button(action: {
print("Button One tapped!")
}) {
Text("First")
}.buttonStyle(PlainButtonStyle()) // << here !!
Spacer()
Button(action: {
print("Button Two tapped!")
}) {
Text("Second")
}.buttonStyle(PlainButtonStyle()) // << here !!
}
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.