Search code examples
swiftuiswiftui-navigationlinkswiftui-navigationview

ActionSheet opens on wrong index than clicked index


Action sheet is not opening to the right clicked index, it always opens to the wrong index. Code snippet is-

Steps are:

1: here passing data to LazyVGrid

2: A View that have some image, text and three dot button

3: A common view that will handle the post tap event

LazyVGrid(columns: SizeConfig.GridLayout.adaptiveGridItemLayout_140) {
     ForEach(folderData) { folderItem in
        MakeGridFolders(folderData: folderItem)
    }
}

@ViewBuilder
private func MakeGridFolders(folderData: FolderModel)-> some View {
    NavigationLink(destination: FilesView()) {
        VStack() {
            Image(App.Image.fileIcon_Light)
            HStack {
                Text(folderData.folderName)
                Spacer()
                MenuButton(isActionSheetShow: $isActionSheetShow, action: {
                    isActionSheetShow.toggle()
                })
            }
        }
    }
}

struct MenuButton: View {
@Binding var isActionSheetShow: Bool
var action: () -> Void

var body: some View {
    VStack {
        Button {
            action()
        } label: {
            Image(icon)
        }
        .confirmationDialog("", isPresented: $isActionSheetShow, titleVisibility: .hidden) {
            //Some buttons
        }
    }
}

Solution

  • You joined all dialogs with one state, so once it is toggled all of them are activated.

    Instead use internal state inside each button, like

    struct MenuButton: View {
    @State private var isActionSheetShow: Bool = false  // << here !!
    var action: () -> Void
    
    var body: some View {
        VStack {
            Button {
                action()
            } label: {
                Image(icon)
            }
            .confirmationDialog("", isPresented: $isActionSheetShow, titleVisibility: .hidden) {
                //Some buttons
            }
        }
    }
    

    *assuming other code you will update correspondingly.