Home > database >  Why I am not able to use multiple `.alert` dialog in my SwiftUI project?
Why I am not able to use multiple `.alert` dialog in my SwiftUI project?

Time:03-02

I want to delete list items and when I delete list items, it will show confirmation dialog like .alert dialog. I have code below and if I want to remove list item .alert dialog is work, but if I try to remove all list items, .alert dialog not work, and I am not able to remove all items, I do not know where I missed? I guess most probably it is due to the I have two .alert dialog and they are conflicted, any idea?

 struct CustomView: View {

    @State private var selectedUsers: CustomModel?
    @State var users: [CustomModel]
    @State private var selectDelete = false
    @State private var selectAllDelete = false

    var body: some View {
        ScrollView(.vertical, showsIndicators: false, content: {  
            VStack(content: {
                ForEach(users){ user in
                    CustomRowView(user: user)
                        .contextMenu {
                            Button(action: {

                            selectDelete = true

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

    private func delete(item user: CustomModel) {
        if let index = users.firstIndex(where: { $0.id == user.id }) {
            users.remove(at: index)
        }
    }
}

model:

struct CustomModel: Identifiable{
var id = UUID().uuidString
var name: String
}

var users = [

CustomModel(name: "david"),
CustomModel(name: "marry"),
CustomModel(name: "henry"),
CustomModel(name: "nadi"), ]

CodePudding user response:

If you apply the modifier to each Button it'll work. Also, you might find confirmationDialog more suitable for this task.

Move your Buttons into custom Views will help too because body has a 10 View limit.

CodePudding user response:

You can create an alert type and handle it using switch statement.

enum AlertType {
    case selectDelete
    case selectAllDelete
}

private var alertType: AlertType?
@State private var isAlertPresented = false

...

Button(action: {
    alertType = .selectDelete
    isAlertPresented = true
}) {
    Text("remove all") 
}

...

.alert(isPresented: $isAlertPresented) {
    presentAlert()
}

...

func presentAlert() -> Alert {
    switch alertType {
    case .selectDelete:
        return Alert(title: Text("title"), 
            message: Text("message"),
            primaryButton: .destructive(Text("Delete")) {
                self.delete(item: data)
            },
            secondaryButton: .cancel())
    case .selectAllDelete:
        return Alert(title: Text("title"), 
            message: Text("message"), 
            primaryButton: .destructive(Text("Delete")) {
                self.datas.removeAll()
            },
            secondaryButton: .cancel())
    default:
        return Alert(title: Text(""))
    }
}
  • Related