【发布时间】:2021-11-18 10:27:34
【问题描述】:
我正在尝试使用 iOS 15.0 swipeActions 和 confirmationDialog 删除 List 中的项目。
但是会发生错误的项目被删除。
这是我的代码:
struct ConversationsSection: View {
@State private var isShowingDeleteActions = false
let items = ["One", "Two", "Three", "Four", "Five"]
var body: some View {
List(items, id: \.self) { item in
Text(item)
.swipeActions(edge: .trailing) {
Button(role: .destructive) {
isShowingDeleteActions = true
print("Trying to delete: " + item)
} label: {
Label("Delete", systemImage: "trash")
}
}
.confirmationDialog("Delete item?", isPresented: $isShowingDeleteActions) {
Button("Confirm Delete", role: .destructive) {
print("Actually deleting: " + item)
isShowingDeleteActions = false
}
}
}
}
}
输出是:
Trying to delete: Two
Actually deleting: Four
Trying to delete: Five
Actually deleting: Three
所以我刷了一个项目并显示confirmationDialog。但是在confirmationDialog 内部传递了另一个项目。这是为什么呢?
【问题讨论】: