Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· openaiHugging Face Suzhen/CodeChat

import SwiftUI struct ContentView: View { var body: some View { VStack { NavigationLink…

  1. User

    import SwiftUI

    struct ContentView: View { var body: some View { VStack { NavigationLink { GameView() } label: { Label("Start game", systemImage: "play.fill") } } } }

    #Preview { ContentView() } but navigation button is disabled and i don't switching ti gameview

  2. Assistant

    The reason why the navigation button is disabled and not switching to the GameView could be because there is no navigation controller available to handle the navigation.

    In SwiftUI, navigation is typically handled within a NavigationView. To fix the issue, wrap your ContentView inside a NavigationView like this:

    import SwiftUI
    
    struct ContentView: View {
        var body: some View {
            NavigationView { // wrap content inside a NavigationView
                VStack {
                    NavigationLink(destination: GameView()) { // use destination parameter instead of label
                        Label("Start game", systemImage: "play.fill")
                    }
                }
            }
        }
    }
    
    struct GameView: View {
        var body: some View {
            Text("Game View")
        }
    }
    
    struct ContentView_Previews: PreviewProvider {
        static var previews: some View {
            ContentView()
        }
    }
    

    With the NavigationView, the navigation button should be enabled, and tapping on it should switch to the GameView.