简体   繁体   English

如何使用 SwiftUI 连续呈现两个警报视图

[英]How to consecutively present two alert views using SwiftUI

I want to immediately present the second alert view after click the dismiss button of the first alert view.单击第一个警报视图的关闭按钮后,我想立即显示第二个警报视图。

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")))}
})
)

However, it gives me an error of 'Alert.Button' is not convertible to 'Alert.Button?'但是,它给了我一个错误“Alert.Button”不能转换为“Alert.Button?” If I put this segment out of the scope of dismissButton, it will override the previous.alert.如果我把这个段放在dismissButton的scope之外,它会覆盖previous.alert。 So how can i do it, I just want to pop up the second alert after clicking the dismiss button of the first alert.那么我该怎么做呢,我只想在单击第一个警报的关闭按钮后弹出第二个警报。 Thanks.谢谢。

It appears (tested with Xcode 11.2):它出现(使用 Xcode 11.2 测试):

  1. While not documented, but it is not allowed to add more than one.alert modifier in one view builder sequence - works only latest虽然没有记录,但不允许在一个视图构建器序列中添加多个.alert 修饰符 - 仅适用于最新
  2. It is not allowed to add.alert modifier to EmptyView, it does not work at all不允许在 EmptyView 中添加.alert 修饰符,它根本不起作用

I've found alternate solution to proposed by @Rohit.我找到了@Rohit 提出的替代解决方案。 In some situations, many alerts, this might result in simpler code.在某些情况下,许多警报,这可能会导致更简单的代码。

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")))
            }
        }
    }
}

backup 备份

Please try below code.请尝试以下代码。

Consecutively present two alert views using SwiftUI使用SwiftUI连续呈现两个警报视图

struct ContentView: View {

    @State var showAlert: Bool = false
    @State var alertIsVisible: Bool = false
    @State var bonusAlertIsVisible: Bool = false

    var body: some View {

        NavigationView {

            Button(action: {
                self.displayAlert()
            }) {
                Text("Hit Me!")
            }
            .alert(isPresented: $showAlert) { () -> Alert in
                if alertIsVisible {
                    return Alert(title: Text("First alert"), message: Text("\n"), dismissButton:.default(Text("Next Round"), action: {
                        DispatchQueue.main.async {
                            self.displayAlert()
                        }
                     })
                   )
                }
                else {
                    return Alert(title: Text("Bonus"), message: Text("You've earned 100 points bonus!!"), dismissButton:.default(Text("Close"), action: {
                                self.showAlert = false
                                self.bonusAlertIsVisible = false
                                self.alertIsVisible = false
                        })
                    )
                }
            }
            .navigationBarTitle(Text("Alert"))
        }
    }

    func displayAlert() {

       self.showAlert = true
       if self.alertIsVisible == false {
            self.alertIsVisible = true
            self.bonusAlertIsVisible = false
       }
       else {
            self.alertIsVisible = false
            self.bonusAlertIsVisible = true
       }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM