How can I use multiple `.alert` dialog in SwiftUI?

I have multiple alert dialog in project, just one of them is work, but I am still do not know why I am not use second one, so how we can use multiple .alert dialog in SwiftUI?

struct MyView: View {

   @State private var selectedUsers: MyModel?
   @State var datas: [MyModel]
   @State private var deleteRow = false
   @State private var deleteRows = false

    var body: some View {
        
            ScrollView(.vertical, showsIndicators: false, content: 
   { 
                
                VStack(content: {
                    
                    ForEach(datas){ data in
  
                        MyRowView(data: data)
                         
                            .contextMenu {

                                Button(action: {
                               deleteRow = true
                                }) {
                                    Text("delete") 
                                }
                                 Button(action: {
                                  deleteRows = true
                                }) {
                                    Text("delete") 
                                }
                            }
                            .onTapGesture {
                                selectedUsers = data
                            
                            }
                   .alert(isPresented: $deleteRow) {
              Alert(title: Text("title"),
                message: Text("message"),
                primaryButton: .destructive(Text("Delete")) {
               
                self.delete(item: data)
                 
                },
                secondaryButton: .cancel())
            }
     .alert(isPresented: $deleteRows) {
              Alert(title: Text("title"),
                message: Text("message"),
                primaryButton: .destructive(Text("Delete")) {
                   self.datas.removeAll()
                },
                secondaryButton: .cancel())
            }
                 
           
                    } .onDelete { (indexSet) in
                       self.datas.remove(atOffsets: indexSet)
                    }})
           })}

    private func delete(item data: MyModel) {
               if let index = datas.firstIndex(where: { $0.id == data.id }) {
                   datas.remove(at: index)
               }
           }}

You already asked this question…

just one of them is work

Could you be more precise:

  • when you select deleteRow, which one do you see ? Do you expect the deleteRows to show ?
  • when you select deleteRows, which one do you see ? Do you expect the deleteRow to show ?

I don't understand what you are trying to achieve:

  • delete for individual row ?
  • and delete of all rows ?

Why do the buttons have the same title ?

here some example but I do not know how I will add two delete method to one .alert dialog

struct AlertInfo: Identifiable {

    enum AlertType {
        case one
        case two
    }
    
    let id: AlertType
    let title: String
    let message: String
}

struct ContentView6: View {
    // 2
    @State private var info: AlertInfo?

    
    var body: some View {
        VStack {
            Button("Alert 1") {
                // 3
                info = AlertInfo(

                    id: .one,
                    title: "Title 1",
                    message: "Message 1")
            }
            Button("Alert 2") {
                // 4
                info = AlertInfo(

                    id: .two,
                    title: "Title 2",
                    message: "Message 2")
            }
        }
        .alert(item: $info, content: { info in // 5

            Alert(title: Text(info.title),
                  message: Text(info.message))
        })
    }
}

I tested, it works.

But alert(item:content:) is deprecated.

They advise to use alert(_:isPresented:presenting:actions:message:)

You could also try some code like this:

struct ContentView: View {
    
    @State private var showAlert1 = false
    @State private var showAlert2 = false
    
    var body: some View {
        
        Button("Tap to show alert1") {
            showAlert1 = true
            showAlert2 = false
        }
        .alert(isPresented: $showAlert1) {
            Alert(
                title: Text("Alert1"),
                message: Text("showAlert1."),
                primaryButton: .default(
                    Text("OK"),
                    action: { print("OK") }
                ),
                secondaryButton: .destructive(
                    Text("Cancel"),
                    action: { print("Cancel") }
                )
            )
        }
        
        Button("Tap to show alert2") {
            showAlert1 = false
            showAlert2 = true
        }
        .alert(isPresented: $showAlert2) {
            Alert(
                title: Text("Alert2"),
                message: Text("showAlert2."),
                primaryButton: .default(
                    Text("OK"),
                    action: { print("OK") }
                ),
                secondaryButton: .destructive(
                    Text("Cancel"),
                    action: { print("Cancel") }
                )
            )
        }
    }
    
}
How can I use multiple `.alert` dialog in SwiftUI?
 
 
Q