ios - 一个 View 中的多个Alerts不能调用SwiftUI

标签 ios swift swiftui

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

但是,即使我正在调用警报并且可以验证正在调用该方法,我的警报也不会显示。

在我的代码中,我有以下结构

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 Access Key."), primaryButton: .destructive(Text("Reset")) {
                    clearToken()
                    clearAccessKey()
                    clearUsername()
                    }, secondaryButton: .cancel(Text("Cancel")))
        }
    }
}

在主视图中只需通过 resetButton() 调用即可。

但是,当我按下此按钮时,尽管 isResetting 等于 true,但警报不会显示。我已验证我的 .alert 方法在控制台打印时被调用

we get here

第一次按下按钮时。

但是,警报永远不会显示。

警报在同一 View 的其他位置正确显示

.alert(isPresented: $failedLogin) {
                        self.password = ""
                        return
                            Alert(title: Text("Invalid Login"), message: Text("Please verify your credentials and try again"), dismissButton: .default(Text("Ok")))
                    }

但是,当名为 failedLogin@State 变量设置为 true 时,永远不会出现上述警报。

我的第一印象是这可能是 Swift 中的一个错误,如果是的话我会向苹果报告。但是,也许我正在做的事情由于我的错误而无法正常工作。

编辑:

正如下面的答案所澄清的,问题似乎与我的 resetButton() 试图在已包含警报的 View 中抛出警报有关。显然,您不能在一个 View 中显示多个警报。

最佳答案

您可以使用 .alert(item:) 代替 .alert(isPresented:) 动态显示提醒:

struct AlertItem: Identifiable {
    var id = UUID()
    var title: Text
    var message: Text?
    var dismissButton: Alert.Button?
}

struct ContentView: View {

    @State private var alertItem: AlertItem?

    var body: some View {
        VStack {
            Button("First Alert") {
                self.alertItem = AlertItem(title: Text("First Alert"), message: Text("Message"))
            }
            Button("Second Alert") {
                self.alertItem = AlertItem(title: Text("Second Alert"), message: nil, dismissButton: .cancel(Text("Some Cancel")))
            }
            Button("Third Alert") {
                self.alertItem = AlertItem(title: Text("Third Alert"))
            }
        }
        .alert(item: $alertItem) { alertItem in
            Alert(title: alertItem.title, message: alertItem.message, dismissButton: alertItem.dismissButton)
        }
    }
}

关于ios - 一个 View 中的多个Alerts不能调用SwiftUI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59116198/

相关文章:

ios - 当我在 Story Boards 中命名我的类(class)时黑屏

ios - 面向 TDD 初学者的 Swift 单元测试

SwiftUI CoreData NSManagedObjectContext 在环境中

ios - SwiftUI NavigationView navigationBarTitle LayoutConstraints 问题

swift - 侧边栏导航中的默认选择

ios - 从 loginviewcontroller 获取用户邮箱并显示在 revealview 的标签中

ios - 使用参数定义长消息字符串的位置

ios - Apple Watch 是否可以直接从 Web 服务器获取数据,而无需 iPhone 作为中介?

ios - TabBarController 中嵌入的导航栏中未显示按钮项

ios - Swift,协议(protocol)和变量值未存储...?