我想将两个独特的alert 附加到同一个Button
视图.当我使用下面的代码时,只有底部的alert 起作用.
我正在macOS Catalina上使用Xcode 11的官方版本.
@State private var showFirstAlert = false
@State private var showSecondAlert = false
Button(action: {
if Bool.random() {
showFirstAlert = true
} else {
showSecondAlert = true
}
}) {
Text("Show random alert")
}
.alert(isPresented: $showFirstAlert) {
// This alert never shows
Alert(title: Text("First Alert"), message: Text("This is the first alert"))
}
.alert(isPresented: $showSecondAlert) {
// This alert does show
Alert(title: Text("Second Alert"), message: Text("This is the second alert"))
}
我希望当我将showFirstAlert
设置为true时,第一个alert 会显示,当我将showSecondAlert
设置为true时,第二个alert 会显示.只有第二个alert 显示其状态为真,但第一个alert 什么也不做.