是否可以在 SwiftUI 菜单上有一个确认对话框?
Is it possible to have a confirmationDialog on SwiftUI Menu?
我见过一个使用 Menu 的应用程序,当您按下 a 按钮时,系统会要求我“确认”。这让我重构了我的应用程序:
@State var confirmDeletion: Bool = false
VStack {
Button(role: .destructive) {
self.confirmDeletion = true
} label: {
Spacer()
Text("Delete")
Spacer()
}.confirmationDialog(
"Are you sure?",
isPresented: $confirmDeletion,
titleVisibility: .visible
) {
Button("Yes", role: .destructive) {
DispatchQueue.main.async {
Task {
await doSomeAsynWork()
}
}
}
Button("Cancel", role: .cancel) {}
}
}
效果很好。现在使用菜单进行重构:
Menu {
[..] // other buttons
Button(role: .destructive) {
print("I was called... and that's it")
self.confirmDeletion = true
} label: {
Label("Delete", systemImage: "trash")
}.confirmationDialog(
"Are you sure?",
isPresented: $confirmDeletion,
titleVisibility: .visible
) {
Button("Yes", role: .destructive) {
DispatchQueue.main.async {
Task {
await doSomeAsynWork()
}
}
}
Button("Cancel", role: .cancel) {}
}
} label: {
Label("Menu", systemImage: "line.horizontal.3.decrease.circle")
}
我知道当您按下任何菜单按钮时,它会立即关闭,这就是 confirmationDialog
不起作用的原因。我可以用 Menu 实现 confirmationDialog
吗?
将它移到 Menu
之外,如
Menu {
[..] // other buttons
Button(role: .destructive) {
print("I was called... and that's it")
self.confirmDeletion = true
} label: {
Label("Delete", systemImage: "trash")
}
} label: {
Label("Menu", systemImage: "line.horizontal.3.decrease.circle")
}
.confirmationDialog( // << here !!
"Are you sure?",
isPresented: $confirmDeletion,
titleVisibility: .visible
) {
Button("Yes", role: .destructive) {
DispatchQueue.main.async {
Task {
await doSomeAsynWork()
}
}
}
Button("Cancel", role: .cancel) {}
}
我见过一个使用 Menu 的应用程序,当您按下 a 按钮时,系统会要求我“确认”。这让我重构了我的应用程序:
@State var confirmDeletion: Bool = false
VStack {
Button(role: .destructive) {
self.confirmDeletion = true
} label: {
Spacer()
Text("Delete")
Spacer()
}.confirmationDialog(
"Are you sure?",
isPresented: $confirmDeletion,
titleVisibility: .visible
) {
Button("Yes", role: .destructive) {
DispatchQueue.main.async {
Task {
await doSomeAsynWork()
}
}
}
Button("Cancel", role: .cancel) {}
}
}
效果很好。现在使用菜单进行重构:
Menu {
[..] // other buttons
Button(role: .destructive) {
print("I was called... and that's it")
self.confirmDeletion = true
} label: {
Label("Delete", systemImage: "trash")
}.confirmationDialog(
"Are you sure?",
isPresented: $confirmDeletion,
titleVisibility: .visible
) {
Button("Yes", role: .destructive) {
DispatchQueue.main.async {
Task {
await doSomeAsynWork()
}
}
}
Button("Cancel", role: .cancel) {}
}
} label: {
Label("Menu", systemImage: "line.horizontal.3.decrease.circle")
}
我知道当您按下任何菜单按钮时,它会立即关闭,这就是 confirmationDialog
不起作用的原因。我可以用 Menu 实现 confirmationDialog
吗?
将它移到 Menu
之外,如
Menu {
[..] // other buttons
Button(role: .destructive) {
print("I was called... and that's it")
self.confirmDeletion = true
} label: {
Label("Delete", systemImage: "trash")
}
} label: {
Label("Menu", systemImage: "line.horizontal.3.decrease.circle")
}
.confirmationDialog( // << here !!
"Are you sure?",
isPresented: $confirmDeletion,
titleVisibility: .visible
) {
Button("Yes", role: .destructive) {
DispatchQueue.main.async {
Task {
await doSomeAsynWork()
}
}
}
Button("Cancel", role: .cancel) {}
}