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 ...")
}
}
}
}