WWDC24 SALE: Save 50% on all my Swift books and bundles! >>

Edit a list item that is a NavigationLink

Forums > SwiftUI

@frios  

I have a list of NavigationLinks that operate as expected. I use EditMode to allow the user to move and delete items in the list. When in edit mode, I want the user to tap on a list item to be able to display a sheet where the list item's content can be modified. This MOSTLY works, but not consistently. below is my code:

    var body : some View {
        List {
            ForEach(connectionViewModel.items.indices, id: \.self) { index in
                NavigationLink (destination: SSHSessionView (connection: connectionViewModel.items[index])) {
                    ConnectionView(index: index)
                }
                //pass tap through the view to allow the NavigationLink to work
                .contentShape(Rectangle())
                .allowsHitTesting(self.editMode == .inactive ? false : true)
                .onTapGesture {
                    if self.editMode.isEditing {
                        self.sheetItem = .editItem
                        self.selectedIndex = index
                    }
                }
            }
            .onDelete(perform: deleteItem)
            .onMove(perform: moveItem)
        }

        .navigationBarTitle((connectionViewModel.url?.lastPathComponent as NSString?)?.deletingPathExtension ?? "Connections", displayMode: sizeClass == .compact ? .large : .inline)

        .toolbar {
            ToolbarItem(placement: .navigationBarLeading) {
                EditButton()
                    .disabled(connectionViewModel.items.count > 0 ? false : true)
            }
            ToolbarItem(placement: .navigationBarTrailing) {
                if editMode.isEditing {
                    if connectionViewModel.items.count > 1 {
                        Button(action: {
                            sortItemsByName()
                        }, label: {
                            Image(systemName: "arrow.up.arrow.down")
                        })
                    }
                } else {
                    Menu {
                        Button("New Connections file...", action: {
                            newFile = true
                            sheetItem = .newFile
                        })
                        Button("Open Connections file...", action: {
                            sheetItem = .openFile
                        })
                        if connectionViewModel.fileOpened {
                            Button("New Connection", action: {
                                sheetItem = .newItem
                            })
                        }

                    } label: {
                        Image(systemName: "plus")
                    }
                }
            }
        }

        .environment(\.editMode, $editMode)

        .sheet(item: $sheetItem) {item in
            switch item {
            case .newFile:
                FilePickerView(callback: self.importedConnections == false ? self.pickedDocuments : self.newDocFromServerList, UTIs: connectionUTIs, newFileURL: FileDataViewModel<Connection>.getBlankFile (fileType : ViewTypes.connections)!)
            case .openFile:
                FilePickerView(callback : self.pickedDocuments, UTIs : connectionUTIs, newFileURL: nil)
            case .newItem:
                AddEditConnectionView (connectionIndex: 0, newConnection: true)
                    .environmentObject(self.connectionViewModel)
            case .editItem:
                AddEditConnectionView (connectionIndex: self.selectedIndex, newConnection: false)
                    .environmentObject(self.connectionViewModel)
            }
        }

    }

The problem is that tapping on the list item when in edit mode does not always work on the first tap and/or it is sensitive to the location of the tap.

I would like to be able to tap anywhere in the list item only ONCE to bring up my editor sheet. What am I doing wrong?

3      

@frios  

Bump. Still stuck on this issue. I have tried the following:

        List {
            // adds awful transition
            ForEach(connectionViewModel.items.indices, id: \.self) { index in
                if self.editMode.isEditing {
                    ConnectionView(index: index)
                    .contentShape(Rectangle())
                    .onTapGesture {
                        self.sheetItem = .editItem
                        self.selectedIndex = index
                    }
                } else {
                    NavigationLink (destination: SSHSessionView (connection: connectionViewModel.items[index])) {
                        ConnectionView(index: index)
                    }
                }
            }
            .onDelete(perform: deleteItem)
            .onMove(perform: moveItem)
        }

But that has some unacceptable side effects.

Anybody have ideas about how to resolve this issue?

3      

I believe the problem to be the conflict with the built-in behavior of edit mode. I does feel a bit counterinuitive, since we are used to swipe actions when in edit mode, and not editing the item directly there. Incidentally it's quite sad that we can't as yet add custom actions.

You could have a special edit button, either as part of your menu or a floating button at the bottom right corner, which disables the navigation links and triggers the edit on tap.

Not sure if this would get you the result you are after, but I think it feels more natural that way.

3      

I've posted topic with a similar issue. Unfortunately, didn't see your post before doing so. It may not help you, but perhaps it'll get renewed interest.

See Having trouble with NavigationLink and EditMode

3      

Save 50% in my WWDC sale.

SAVE 50% To celebrate WWDC24, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.