swiftuialert

Why don't I need to toggle the isPresented variable myself for an alert?


Am working with an .alert in SwiftUI with the code below. I found that I do not need to toggle the isPresented variable myself within the buttons' closures.

Just need to make sure this is standard, and I'm not missing something else here.

.alert(isPresented: $showingAlert) {
    Alert(  title: Text(""),
            message: Text(alertMsg),
            primaryButton: Alert.Button.default(Text("YES")) {
//                      showingAlert = false
                                                             },
            secondaryButton: Alert.Button.default(Text("NO")) {
//                      showingAlert = false
                                                            })
}

Solution

  • This is documented behavior:

    isPresented
    A binding to a Boolean value that determines whether to present the alert. When the user presses or taps one of the alert’s actions, the system sets this value to false and dismisses.

    The alert modifier being used in the question is deprecated, but the documentation relating to that version is essentially the same.