gpt4 book ai didi

ios - 如何在 SwiftUI 中向 List 的 .ondelete() 添加确认

转载 作者:行者123 更新时间:2023-12-04 12:24:01 46 4
gpt4 key购买 nike

如何向我的 deleteRow() 添加确认信息功能?
A 已经添加了 .alert但我不知道如何继续。

List {
ForEach(data, id: \.self) { item in
Text(item)
.alert(isPresented: self.$showingDeleteAlert) {
Alert(title: Text("..."), message: Text("..."), primaryButton: .destructive(Text("Delete")) {
deleteRow // ???
}, secondaryButton: .cancel()
)
}
}
.onDelete(perform: deleteRow)
}

func deleteRow(at indexSet: IndexSet) {
for index in indexSet {
let item = data[index]
viewContext.delete(item)
do {
try viewContext.save()
} catch let error {
print("Error: \(error)")
}
}
}
``

最佳答案

这是可能的方法:

@State private var toBeDeleted: IndexSet?

...

List {
ForEach(data, id: \.self) { item in
Text(item)
.alert(isPresented: self.$showingDeleteAlert) {
Alert(title: Text("..."), message: Text("..."), primaryButton: .destructive(Text("Delete")) {
for index in self.toBeDeleted {
let item = data[index]
viewContext.delete(item)
do {
try viewContext.save()
} catch let error {
print("Error: \(error)")
}
}
self.toBeDeleted = nil
}, secondaryButton: .cancel() {
self.toBeDeleted = nil
}
)
}
}
.onDelete(perform: deleteRow)
}

func deleteRow(at indexSet: IndexSet) {
self.toBeDeleted = indexSet // store rows for delete
self.showingDeleteAlert = true
}

关于ios - 如何在 SwiftUI 中向 List 的 .ondelete() 添加确认,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62720595/

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