Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

NavigationLink not working inside a button

I have added a ToolBarItemGroup with a button that should navigate to another view.

But in this case the button with navigationLink does not work

It is because I am not able to add a navigationLink to .toolbar?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

VStack(){
    NavigationView{
         List{

         }.toolbar{
             ToolbarItemGroup(placement: .navigationBarTrailing){
                 Button("MyButton"){
                      NavigationLink(Destination: ContentView()){
                           ContentView()
                           }
                   }
              }
         }

Whenever I click the MyButton, it does not navigate to my ContentVieYew
What I am doing wrong here?
I want to use the button in the toolbar

>Solution :

NavigationLink is actually a type of button, albeit one with different constructors and default styling (for example, when you use it inside a List).

In your example code, you’re putting inside another button’s action closure, which should contain code to execute rather than SwiftUI views.

Your toolbar can be more simply expressed as:

.toolbar {
  NavigationLink("MyButton") {
    ContentView()
  }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading