在 swiftUI 中调用警报的方法应该比以往更简单,一个简单的例子是
struct ContentView: View {
@State private var showingAlert = false
var body: some View {
Button(action: {
self.showingAlert = true
}) {
Text("Show Alert")
}
.alert(isPresented: $showingAlert) {
Alert(title: Text("Important message"), message: Text("Wear sunscreen"), dismissButton: .default(Text("Got it!")))
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是,即使我正在调用警报并且可以验证正在调用该方法,我的警报也不会显示。
在我的代码中,我有以下结构
private struct resetButton: View {
@State var isResetting = false
var body: some View {
Button("Reset"){
self.isResetting = true
}.alert(isPresented: $isResetting) {
print("We get here")
return
Alert(title: Text("Are you sure?"), message: Text("This will erase your …Run Code Online (Sandbox Code Playgroud) 我想将两个唯一的警报附加到同一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 …