SwiftUI - Alert doesn't dismiss. It keeps coming back

Viewed 748
.alert(isPresented:$showingAlert) {
        Alert(
            title: Text("Are you sure you want to delete this?"),
            message: Text("There is no undo"),
            primaryButton: .destructive(Text("Delete")) {
                //(using showingAlert = false doesn't help.)
                //(leaving this section empty doesn't help as well so any code written here is unrelated.)
                unrelatedCode()
            },
            secondaryButton: .cancel()
        )
    }

Why does this code result in alert coming back to screen no matter which button I press on it? Here's a vid for easier understanding: https://imgur.com/a/hN5yC3t

Edit: Seems like a bug on swiftui's side I guess. Guess I'll work around it.

Edit 2: Everything that could be related to the issue:

struct EditProcessView: View {
@State var problem:Bool = false
@State var showingAlert = false

@State var text = ""
var body: some View {
    Form{
        //2 unrelated pickers.
        
        //Unrelated textfield
        Button(“Delete”){
            showingAlert = true //Are you sure you want to delete this? Button.
        }
    }
    .frame(minWidth: 200, idealWidth: 200)
    .alert(isPresented: $problem) {Alert.init(title: Text(“Fill the textfield with a number.“))}
    .alert(isPresented:$showingAlert) {
        Alert(
            title: Text("Are you sure you want to delete this?"),
            message: Text("There is no undo."),
            primaryButton: .destructive(Text("Delete")) {
                //Unrelated code.
               
            },
            secondaryButton: .cancel()
        )
    }
}
1 Answers

The alert handles its own dismissal when the user taps one of the buttons in the alert, by setting the bound isPresented value back to false.

@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
            )
        )
    }
}
Related