4
.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://i.stack.imgur.com/qSe9r.jpg

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()
        )
    }
}
Shazniq
  • 423
  • 4
  • 16

1 Answers1

0

Fatemeh's answer solved the issue, I faced the same issue in iOS 15. And in iOS 16, I can use .toogle() in the button to dismiss the alert with no issue.

.alert(Text("Instruction"), isPresented: $showHelp, actions: {
  Button {
   showHelp.toggle() //need to remove this in iOS 15, but no need to remove in iOS 16
  } label: {
    Text("OK")
  }
}, message: {
  Text("some text")
})
Sanjo
  • 41
  • 4