How we can use alert menu before delete list items in SwiftUI?

I have list items in SwiftUI, and when I delete list items I want to delete after alert menu, like "do want to delete your list items, ""yes" or "no" is it possible?

struct MyView: View {

    @State private var selectedUsers: MyModel?
 
    var body: some View {
        
            ScrollView(.vertical, showsIndicators: false, content: {
                
                VStack(content: {
                    
                    ForEach(datas){ data in
  
                        MyRowView(data: data)
                         
                            .contextMenu {

                                Button(action: {
                                    self.delete(item: data)
                                }) {
                                    Text("delete") 
                                }
                               
                            }
                            .onTapGesture {
                                selectedUsers = data
                            
                            }
                        
                    } .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)
               }
           }}
Answered by Claude31 in 706005022

You should create a state var to show alert.

The delete Button will in fact toggle this var and present alert.

Delete action will be done by the alert buttons.

See details here:

https://www.hackingwithswift.com/quick-start/swiftui/how-to-add-actions-to-alert-buttons

Accepted Answer

You should create a state var to show alert.

The delete Button will in fact toggle this var and present alert.

Delete action will be done by the alert buttons.

See details here:

https://www.hackingwithswift.com/quick-start/swiftui/how-to-add-actions-to-alert-buttons

You will have to create another state variable unfortunately, holding indexSet for you.

How we can use alert menu before delete list items in SwiftUI?
 
 
Q