相关疑难解决方法(0)

如何在SwiftUI的一个视图上显示两个警报?

我想将两个唯一的警报附加到同一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"))
}
Run Code Online (Sandbox Code Playgroud)

我希望在设置showFirstAlert为true 时显示第一个警报,而在我设置为true时希望显示第二个警报showSecondAlert …

swift swiftui

4
推荐指数
5
解决办法
251
查看次数

标签 统计

swift ×1

swiftui ×1