Alert

12/5/2021 swiftiosswiftui

# Alert

文档:https://developer.apple.com/documentation/swiftui/alert (opens new window)

一个展示警告信息的容器。

我们可以根据布尔值显示 Alert

@State var isError: Bool = false

Button("Alert") {
    self.isError = true
}.alert(isPresented: $isError, content: {
    Alert(title: Text("Error"), message: Text("Error Reason"), dismissButton: .default(Text("OK")))
})

它也可与 Identifiable 项目绑定

@State var error: AlertError?

var body: some View {
    Button("Alert Error") {
        self.error = AlertError(reason: "Reason")
    }.alert(item: $error, content: { error in
        alert(reason: error.reason)
    })
}

func alert(reason: String) -> Alert {
    Alert(title: Text("Error"),
            message: Text(reason),
            dismissButton: .default(Text("OK"))
    )
}

struct AlertError: Identifiable {
    var id: String {
        return reason
    }

    let reason: String
}
上次更新: 6/18/2022, 10:39:15 AM