SwiftUI Alert

SwiftUI Alert

在以下示例中,点击按钮时会通过更新绑定到警报的本地属性来显示简单警报

@State private var showAlert = false
var body: some View {
    Button("Tap to show alert") {
        showAlert = true
    }
    .alert(isPresented: $showAlert) {
        Alert(
            title: Text("Current Location Not Available"),
            message: Text("Your current location can’t be " +
                            "determined at this time.")
        )
    }
}

要自定义警报,请添加该类型的实例,该实例为诸如取消和执行破坏性操作之类的常见任务提供标准化按钮。以下示例使用两个按钮:标有“重试”的默认按钮调用方法,以及一个“破坏性”按钮调用方法。

@State private var showAlert = false
var body: some View {
    Button("Tap to show alert") {
        showAlert = true
    }
    .alert(isPresented: $showAlert) {
        Alert(
            title: Text("Unable to Save Workout Data"),
            message: Text("The connection to the server was lost."),
            primaryButton: .default(
                Text("Try Again"),
                action: saveWorkoutData
            ),
            secondaryButton: .destructive(
                Text("Delete"),
                action: deleteWorkoutData
            )
        )
    }
}

你可能感兴趣的:(swift)