How to add Context Menu into NavigationLink using SwiftUI?

Viewed 110

I'm trying to add contextMenu into NavigationLink, using this code:

   NavigationLink(recent.name, destination: TextEditing(isNew: false, fileName: recent.name)).contextMenu{Button("Rename") {
                   // code
                    }
                   }

, but i receive swift compiler error: The compiler is unable to type-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions

1 Answers

Try putting your code to execute in a separate function in your view struct and then call it from the context menu:

// put this in your view struct
func action() {
    // code to run your menu action
}

And then call it from your context menu

NavigationLink(recent.name, destination: TextEditing(isNew: false, fileName: recent.name)).contextMenu {
                Button("Rename", action: action)
            }
Related