I want to have two unique alerts attached to the same Button
view. When I use the code below, only the alert on the bottom works.
I'm using the official release of Xcode 11 on macOS Catalina.
@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"))
}
I expect first alert to show when I set showFirstAlert
to true and I expect the second alert to show when I set showSecondAlert
to true. Only the second alert shows when its state is true but the first one does nothing.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…