-1

I tried adding a NavigationLink without a NavigationView on another project and it worked.

However when I tried copy and pasting the code onto another project, it doesn't work.

Please help. I've tried testing it on an empty project and it doesn't work as well. I'm wondering what went wrong. I've checked the curly braces placement as well. All placed correctly...

import SwiftUI


struct Intro: View {
var body: some View {
    
    ZStack {
        VStack {
            Image("CDM")
                .resizable()
                .frame(width: 500, height: 500, alignment: .center)
            HStack {
                Text("Welcome to Chatter")
                    .font(.system(size: 45, weight: .semibold, design: .rounded))
                    .foregroundColor(Color("Yellow"))
                Image(systemName: "message.fill")
                    .font(.system(size: 45))
                    .foregroundColor(Color("Yellow"))
                    .padding()
            }
            
            Text("""
             Note:
             This experience is best viewed in Horizontal
             """)
            .font(.system(size: 30, weight: .semibold, design: .rounded))
            .foregroundColor(.secondary)
            .padding()
            
            
            
       

            Button(action: {
                print("intro2")
            }) {
                NavigationLink(destination: Intro2()) {
                    Text("Next")

                        .font(.system(size: 30, weight: .semibold ,design: .rounded))
                        .foregroundColor(.white)
                        .padding()
                        .background(Color("Purple"))
                        .cornerRadius(20)
                }
            }
        }
        
    }
    
    
}
}


struct Intro2: View {
var body: some View {
    VStack {
        Text("What you can do in this experience")
        
        Button {
            print("test")
        } label: {
            NavigationLink(destination: Main()) {
                HStack {
                    Text("Click here to get started!")
                        .font(.system(size: 25, weight: .regular ,design: .rounded))
                        .foregroundColor(.white)
                        .padding()
                        .background(Color("Purple"))
                        .cornerRadius(20)
                }
                
            }
        }
    }
}

}

54Hex
  • 47
  • 8
  • "...I tried adding a NavigationLink without a NavigationView on another project and it worked...", no it does not. There is no need to use a `Button`, `NavigationLink` is already a button. You need a `NavigationView` somewhere up your hierarchy. – workingdog support Ukraine Jun 08 '22 at 06:16
  • 1
    Hm. On my other project it uses this Button {} label: { NavigationLink(destination: MW()) { HStack { Text("Ready? Here we go!") .font(.system(size: 35, weight: .light ,design: .rounded)) .padding() .background(Color("midnight")) .cornerRadius(20) } } } and it worked without a NavigationView... Is there a workaround? – 54Hex Jun 08 '22 at 06:20
  • the action of the button that you show, will be done, for example `print("test")` or `print("intro2")`, but it does not "take" you to the `NavigationLink` destination, such as `Main()`, at least not for me on my system. You need a `NavigationView` somewhere up your hierarchy to work. – workingdog support Ukraine Jun 08 '22 at 07:12
  • The purpose of the print under the action: was to test out if the button works or not. I know it won't affect the Navlink – 54Hex Jun 08 '22 at 10:22

1 Answers1

1

here is an example code, that shows the NavigationLink is not triggered if it is not inside a NavigationView. Comment-out the NavigationView and see the difference.

import SwiftUI

@main
struct TestApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}


struct ContentView: View {
    var body: some View {
        NavigationView {
            Button { print("testing") } label: {
                NavigationLink(destination: Text("Intro link view")) {
                    Text("Click here to get started!")
                }
            }
        }
    }
}

Note that NavigationView will be deprecated, see https://developer.apple.com/documentation/swiftui/navigationview

As I mentioned, there is no need to use a Button, NavigationLink is already a button.

  • Ah thanks so much! I have found a better way by adding .navigationViewStyle(StackNavigationViewStyle()) because I don't want the view to appear at the side.. – 54Hex Jun 10 '22 at 08:49
  • where did you add `navigationViewStyle`, since you are telling us you don't have a `NavigationView`? Did you added to the `Button`? – workingdog support Ukraine Jun 10 '22 at 09:28
  • I added it under “MyApp” and on previews as well. Then added navigation view and it worked. – 54Hex Jun 11 '22 at 10:57
  • you were telling us all works without a `NavigationView`, now you saying you had to add one to make it work. `Hm. On my other project ....and it worked without a NavigationView`. what a lot of BS that was. – workingdog support Ukraine Jun 11 '22 at 12:15
  • I'm not exactly sure why that other project worked without a navigation view. and the other needed one. And I'm still rather new to swift. – 54Hex Jun 13 '22 at 04:07
  • I believe that the other project contained a nav view somewhere and hence it worked. I was asking this question because I don't want my view to appear on the sidebar and hence found out that I can use a stacked navigation style to remove the sidebar. Since a Nav View is needed for a Nav Link to work. – 54Hex Jun 13 '22 at 04:10