【问题标题】:How to consecutively present two alert views using SwiftUI如何使用 SwiftUI 连续呈现两个警报视图
【发布时间】:2019-11-13 09:54:21
【问题描述】:

我想在单击第一个警报视图的关闭按钮后立即显示第二个警报视图。

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' is not convertible to 'Alert.Button?' 如果我将此段置于dismissButton 的范围之外,它将覆盖之前的.alert。 那么我该怎么做呢,我只想在单击第一个警报的关闭按钮后弹出第二个警报。 谢谢。

【问题讨论】:

  • 这能回答你的问题吗? How can I have two alerts on one view in SwiftUI?
  • 不,不一样,我也试过那个,但是你需要点击两次“Hit me”按钮才能弹出第二个警报视图,我想点击第一个警报的关闭按钮

标签: ios swift swiftui


【解决方案1】:

它出现了(用 Xcode 11.2 测试):

  1. 虽然没有记录,但不允许添加多个 一个视图构建器序列中的 .alert 修饰符 - 仅适用于最新版本
  2. 不允许在 EmptyView 中添加 .alert 修饰符,它不起作用 完全没有

我找到了@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()
    }
}

【讨论】:

  • 干杯人!你能告诉我当我们添加这部分代码时会发生什么:DispatchQueue.main.async { self.bonusAlertIsVisible = true }?
  • 这个状态会在主循环的下一个事件中改变,所以不会和第一个alert冲突,仍然是打开的。如果同步改变这个状态是不行的,因为这里不能有两个打开的alert。
【解决方案2】:

请尝试以下代码。

使用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
       }
    }
}

【讨论】:

  • 天才!所以就像我们在主调度队列中异步添加 $showAlert=false 一样,它会显示 .alert 两次而不需要单击按钮?
猜你喜欢
  • 1970-01-01
  • 2021-01-06
  • 1970-01-01
  • 2020-01-23
  • 1970-01-01
  • 2016-07-12
  • 1970-01-01
  • 1970-01-01
  • 2017-01-27
相关资源
最近更新 更多