I want to present Confirmation Dialog with 2 different binding properties, something like this:
.confirmationDialog("", isPresented: $calendarViewModel.showingDeleteEventDialog || $remindersViewModel.showingDeleteReminderDialog) {
Button("Delete", role: .destructive) {
switch detailType {
case .reminder:
remindersViewModel.deleteReminder()
case .event:
calendarViewModel.deleteEvent()
}
}
Button("Cancel", role: .cancel) { }
} message: {
Text("Are you sure you want to delete this (correctName.lowercased())?")
}
I don’t want to create 2 separate confirmation dialogs, if it is possible how can I bind 2 properties in one, so if one of them is true, dialog is opening up and automatically when it’s closed turn them into false
2
Answers
I would go with an enum with a computed property for this
And then replace the two boolean properties you are using now with a single one of type
ShowDialog
If you use
confirmationDialog(_:isPresented:titleVisibility:presenting:actions:message:)
then you can supply the data for the dialog using thepresenting
parameter.The documentation says:
What this means, is that you would need a boolean state variable for controlling the visibility of the dialog and another state variable for controlling the type of dialog. Although there would still be two state variables, you won’t need any more than that, even if there would be more types.
Like this: