TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Navigation link under context menu problems

Forums > SwiftUI

Here is the code I have right now

@State private var selectedBook: Book? = nil

//...other code

ForEach(bookData){ bookDetail in
    BookView(book: bookDetail)
    .background(NavigationLink(destination: EditBook(book: bookDetail), tag: bookDetail, selection: $selectedBook){ EmptyView() })
    .contextMenu{
       Button(action: {
           self.selectedBook = bookDetail
       }) {
           Label("Edit", systemImage: "pencil")
          }

       Button(action: {
           //Delete action
       }) {
           Label("Delete", systemImage: "trash")
          }
    }

The reason the code is written this way is because the navigation link wasn't working if I put it directly under the context menu. My problem is that the .background modifier opens the view in the navigation link on the tap of the view it's bound to as well as the tap of the context menu. I need the tap action to open a different view so I want the current nav link to open only at a press of the context menu.

3      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

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.