使用
alert
接受要呈现的值的重载,例如
alert(_:isPresented:presenting:actions:)
或
alert(item:content:)
(在iOS 17中已弃用)。通过这种方式,您可以移动
警觉的
修改器位于
Table
相反
添加新的
@State
以存储要删除的项目,并在上下文菜单“删除”按钮的操作中设置此项。
// assuming secretItems is an array of "Item"
@State private var toBeDeletedItem: Item?
.contextMenu {
Button("Delete Item") {
showDeleteAlert.toggle()
toBeDeletedItem = item
}
}
.alert("Do you want to delete item?", isPresented: $showDeleteAlert, presenting: toBeDeletedItem) { item in
Button("Delete", role: .destructive) {
deleteSelectedSecretItem(item)
}
Button("Cancel", role: .cancel) { }
}
或者,如果您使用的是早期版本,并且
警报(_:呈现:呈现:操作:)
不可用:
.alert(item: $toBeDeletedItem) { item in
Alert(
title: Text("Do you want to delete item?"),
message: Text(""),
primaryButton: .cancel(
Text("Cancel"),
action: { }
),
secondaryButton: .destructive(
Text("Delete"),
action: { deleteSelectedSecretItem(item) }
)
)
}