我希望在同一个Button
视图上附加两个不同的警报。当我使用下面的代码时,只有底部的警报能够正常工作。
我正在使用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时,我希望第一个警报显示出来,并且当我将showSecondAlert
设置为true时,我希望第二个警报显示。只有第二个警报的状态为true时才会显示,但第一个警报则什么都不做。
showFirstAlert
或showSecondAlert
设置为false
! - LinusGeffarth.alert
显示消息时,这种情况特别隐蔽。(这让我想起了 UIKit 中UIAlertView
的问题 - 尝试在视图控制器层次结构的多个级别中使用它们)。 - Chris Prince