No more than one Alert in a View?

My code below seems to say no. I think one should be able to have more than one alert in a view.


struct ContentView : View {
    @State private var showingAlert = false
    @State private var showSavedAlert = false
    
    var body: some View {
        VStack {
            Text("Hello Everybody!")
            Button(action: {
                self.showingAlert = true
            }) {
                Text("Save")
            }
            Button(action: {
                self.showSavedAlert = true
            }) {
                Text("Another Save")
            }
            
        }
        .alert(isPresented: $showingAlert) {
            Alert(title: Text("Save File?"), message: Text("Do you want to save?"), primaryButton: .default(Text("Save")), secondaryButton: .cancel())
        }
        .alert(isPresented: $showSavedAlert) {
            Alert(title: Text("Another Save File?"), message: Text("Do you really want to save?"), primaryButton: .default(Text("Save")), secondaryButton: .cancel())
        }
    }
}

Replies

Same issue, I think it's a bug.

Could you try a different design ?


Define

    @State private var alertTitle = ""
    @State private var message = ""


Change action(s):

            Button(action: {
                self.showingAlert = true
                self.alertTitle = "Save File?"
                self.message = "Do you want to save?"
            }) {


And call the alert only once

.alert(isPresented: $showingAlert || $showSavedAlert) {
            Alert(title: Text($alertTitle), message: Text($message), primaryButton: .default(Text("Save")), secondaryButton: .cancel())
        }
}

Note: I cannot test this code, not totally sure of exact syntax.