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

Replies

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 ?

  • @Claude31, it is not same question, I mean .alert not work for two button, it is just work for one, for example it delete all row, but for singel row it is not work, the problem is due to I have two .aler dialog, I have to use it with switch case I guess, but how?

Add a Comment

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 don't understand your code. Where is alert presented ?

  • @Claude31, it example code I found on internet, I guess I have to write my code according to this example, but I do not know how?

  • @Claude31, how I will use   self.delete(item: data),      and   self.datas.removeAll() inside of the here ` .alert(item: $info, content: { info in // 5

    Alert(title: Text(info.title), message: Text(info.message)) })`
Add a Comment

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") }
                )
            )
        }
    }
    
}
  • @Claude31, inside of the .context menu .alert not work I have to write outside, but in outside just one .alert work

  • @Claude31, is there any way?

Add a Comment