Ios 如何使用SwiftUI连续显示两个警报视图

Ios 如何使用SwiftUI连续显示两个警报视图,ios,swift,swiftui,Ios,Swift,Swiftui,我想在单击第一个警报视图的“解除”按钮后立即显示第二个警报视图 Button(action: { self.alertIsVisible = true }) { Text("Hit Me!") } .alert(isPresented: $alertIsVisible) { () -> Alert in return Alert(title: Text("\(title)"), message: Text("\n"), dismissButton:.defaul

我想在单击第一个警报视图的“解除”按钮后立即显示第二个警报视图

Button(action: {
     self.alertIsVisible = true
}) {
     Text("Hit Me!")
}
.alert(isPresented: $alertIsVisible) { () -> Alert in
    return Alert(title: Text("\(title)"), message: Text("\n"), dismissButton:.default(Text("Next Round"), action: {
        if self.score == 100 {
            self.bonusAlertIsVisible = true
    }
    .alert(isPresented: $bonusAlertIsVisible) {
        Alert(title: Text("Bonus"), message: Text("You've earned 100 points bonus!!"), dismissButton: .default(Text("Close")))}
})
)
但是,它给了我一个错误“Alert.Button”不能转换为“Alert.Button” 如果我将此段放在dismissButton的范围之外,它将覆盖上一个.alert。 那么我该怎么做呢?我只想在单击第一个警报的“解除”按钮后弹出第二个警报。 谢谢。

请尝试下面的代码

使用SwiftUI连续显示两个警报视图

请尝试下面的代码

使用SwiftUI连续显示两个警报视图


它似乎是用Xcode 11.2测试的:

虽然未记录,但不允许添加多个 .一个视图生成器序列中的警报修饰符-仅适用于最新版本 不允许将.alert修饰符添加到EmptyView,它不起作用 完全 我找到了@Rohit提出的替代解决方案。在某些情况下,许多警报可能会导致更简单的代码

struct TestTwoAlerts: View {
    @State var alertIsVisible = false
    @State var bonusAlertIsVisible = false

    var score = 100
    var title = "First alert"

    var body: some View {
        VStack {
            Button(action: {
                 self.alertIsVisible = true
            }) {
                 Text("Hit Me!")
            }
            .alert(isPresented: $alertIsVisible) {
                Alert(title: Text("\(title)"), message: Text("\n"), dismissButton:.default(Text("Next Round"), action: {
                    if self.score == 100 {
                        DispatchQueue.main.async { // !! This part important !!
                            self.bonusAlertIsVisible = true
                        }
                    }
                }))
            }
            Text("")
            .alert(isPresented: $bonusAlertIsVisible) {
                    Alert(title: Text("Bonus"), message: Text("You've earned 100 points bonus!!"), dismissButton: .default(Text("Close")))
            }
        }
    }
}

struct TestTwoAlerts_Previews: PreviewProvider {
    static var previews: some View {
        TestTwoAlerts()
    }
}

它似乎是用Xcode 11.2测试的:

虽然未记录,但不允许添加多个 .一个视图生成器序列中的警报修饰符-仅适用于最新版本 不允许将.alert修饰符添加到EmptyView,它不起作用 完全 我找到了@Rohit提出的替代解决方案。在某些情况下,许多警报可能会导致更简单的代码

struct TestTwoAlerts: View {
    @State var alertIsVisible = false
    @State var bonusAlertIsVisible = false

    var score = 100
    var title = "First alert"

    var body: some View {
        VStack {
            Button(action: {
                 self.alertIsVisible = true
            }) {
                 Text("Hit Me!")
            }
            .alert(isPresented: $alertIsVisible) {
                Alert(title: Text("\(title)"), message: Text("\n"), dismissButton:.default(Text("Next Round"), action: {
                    if self.score == 100 {
                        DispatchQueue.main.async { // !! This part important !!
                            self.bonusAlertIsVisible = true
                        }
                    }
                }))
            }
            Text("")
            .alert(isPresented: $bonusAlertIsVisible) {
                    Alert(title: Text("Bonus"), message: Text("You've earned 100 points bonus!!"), dismissButton: .default(Text("Close")))
            }
        }
    }
}

struct TestTwoAlerts_Previews: PreviewProvider {
    static var previews: some View {
        TestTwoAlerts()
    }
}

这回答了你的问题吗?不,这是不同的,我也试过了,但是你需要点击两次“点击我”按钮来弹出第二个警报视图,我想点击第一个警报的“解除”按钮这是否回答了你的问题?不,这是不同的,我也尝试过,但你需要点击两次点击我按钮弹出第二个警报视图,我想点击第一个警报的解除按钮!因此,如果我们将$showAlert=false轻松添加到主dispatchqueue中,它将显示.alert两次,而无需单击按钮?Genius man!因此,如果我们将$showAlert=false轻松添加到主dispatchqueue中,它将显示.alert两次,而无需单击按钮?干杯!您能告诉我添加这部分代码时会发生什么:DispatchQueue.main.async{self.bonusAlertIsVisible=true}?此状态将在主循环的下一个事件中更改,因此不会与仍然打开的第一个警报冲突。如果同步更改此状态,它将不起作用,因为这里不能有两个打开的警报。干杯!您能告诉我添加这部分代码时会发生什么:DispatchQueue.main.async{self.bonusAlertIsVisible=true}?此状态将在主循环的下一个事件中更改,因此不会与仍然打开的第一个警报冲突。如果同步更改此状态,它将无法工作,因为此处不能有两个打开的警报。