Can't get NavigationLinks to work with Menu control in iOS 14

Hi,

I am trying to get a navbar menu item which should open a new view when selected but I can't get NavLink to work with Menu.

Code Block
.toolbar {
         ToolbarItem(placement: .navigationBarLeading) {
          Menu {
            NavigationLink(destination: TestView()) {
              Text("Test Navlink")
            }
          }
          label: {
            Label("Add", systemImage: "plus")
          }
        }
}


Aside from seeing this warning:
Code Block
[UILog] Called -[UIContextMenuInteraction updateVisibleMenuWithBlock:] while no context menu is visible. This won't do anything.


I don't see any other error or warning in debugger. Code runs but nothing happens when you tap the menu item. It doesn't open the new view.

Any assistance would be greatly appreciated.
NavigationLink needs to be within the NavigationView hierarchy so you’ll need to be a bit creative with this.

Something like:

Code Block Swift
struct YourView: View {
@State private var isActive = false
@State private var goTo = “”
var body: some View {
NavigationView {
etc.
}
.background(
NavigationLink(destination: Text(self.goTo), isActive: $isActive) {
EmptyView()
})
}
}

And then for the toolbar all you need to do is:

Code Block Swift
.toolbar {         
ToolbarItem(placement: .navigationBarLeading) { 
          Menu {         
Button("Go to 1") {
self.goTo = "Destination 1"
self.isActive = true
}
Button("Go to 2") {
self.goTo = "Destination 2"
self.isActive = true
}        
}         
  label: {            
Label("Add", systemImage: "plus")          
}        
}
}

Can’t get the indentation right for your code, for some reason the editor doesn’t play nice, but I think you’ll be able to figure it out.

Can't get NavigationLinks to work with Menu control in iOS 14
 
 
Q