🎛️Confirmation Dialog
Do not need to add a cancel button, the confirmation dialog includes a standard dismiss action by default.
All actions in the confirmation dialog dismiss the dialog, do not need to manually dismiss the dialog by setting the boolean state variable to false.

struct ConfirmView: View {
    
    // ⭐️ confirmation dialog on/off switch
    @State private var isShowingDialog = false
    
    var body: some View {
        Button("Delete", role: .destructive) {
            isShowingDialog = true                // ⭐️ show dialog
        }
        .buttonStyle(.borderedProminent)
        
        // ------------------------------
        //     ⭐️ confirmation dialog
        // ------------------------------
        .confirmationDialog(
            "Are you sure to delete the data?",    // title
            isPresented: $isShowingDialog,         // on/off
            titleVisibility: .visible
        ) {
            Button("confirm", role: .destructive) {
                print("data deleted ...")
            }
        }
    }
}use Button Roles.
Last updated
Was this helpful?