Navigation in SwiftUI

49 Views Asked by At

I am new in ios development and have an issue with navigation thing. I am making 15 puzzle app. From main menu (ContentView) i need to start save game, random game and get to list of saved games. Save game and random game lead to one UI but initialize game engine differently and list of saved games lead to another UI where is a list of saved games. From there i want to be able to start any saved game - lead to game view.

I somehow figured out the way to do that, but it's outdated and i don't understand how to implement navigation stack here. Thank you!

This is what i made: Main menu:

struct ContentView: View {
    @EnvironmentObject var puzzleEngine: PuzzleEngine
    @State private var isGameViewActive = false
    @State private var isSavedGamesViewActive = false

    var body: some View {
        NavigationStack {
            VStack {
                NavigationLink(
                    destination: GameSwiftUIView(),
                    isActive: $isGameViewActive,
                    label: {
                        EmptyView()
                    })
                    .hidden()

                NavigationLink(
                    destination: SavedGamesSwiftUIView(),  // Add this destination
                    isActive: $isSavedGamesViewActive,
                    label: {
                        EmptyView()
                    })
                    .hidden()

                Button(action: {
                    puzzleEngine.initializeSaveGame()
                    isGameViewActive = true
                }) {
                    Text("Start Save Game")
                        .bold()
                        .foregroundColor(.primary)
                        .frame(width: 200, height: 50, alignment: .center)
                        .background(Color.secondary)
                        .foregroundColor(.primary)
                        .cornerRadius(50)
                }

                Button(action: {
                    puzzleEngine.initializeRandomGame()
                    isGameViewActive = true
                }) {
                    Text("Start Random Game")
                        .bold()
                        .foregroundColor(.primary)
                        .frame(width: 200, height: 50, alignment: .center)
                        .background(Color.secondary)
                        .foregroundColor(.primary)
                        .cornerRadius(50)
                }

                Button(action: {
                    isSavedGamesViewActive = true
                }) {
                    Text("View Saved Games")
                        .bold()
                        .foregroundColor(.primary)
                        .frame(width: 200, height: 50, alignment: .center)
                        .background(Color.secondary)
                        .foregroundColor(.primary)
                        .cornerRadius(50)
                }
            }
        }
    }
}

and saved games list:

struct ContentView: View {
    @EnvironmentObject var puzzleEngine: PuzzleEngine
    @State private var isGameViewActive = false
    @State private var isSavedGamesViewActive = false

    var body: some View {
        NavigationStack {
            VStack {
                NavigationLink(
                    destination: GameSwiftUIView(),
                    isActive: $isGameViewActive,
                    label: {
                        EmptyView()
                    })
                    .hidden()

                NavigationLink(
                    destination: SavedGamesSwiftUIView(),  // Add this destination
                    isActive: $isSavedGamesViewActive,
                    label: {
                        EmptyView()
                    })
                    .hidden()

                Button(action: {
                    puzzleEngine.initializeSaveGame()
                    isGameViewActive = true
                }) {
                    Text("Start Save Game")
                        .bold()
                        .foregroundColor(.primary)
                        .frame(width: 200, height: 50, alignment: .center)
                        .background(Color.secondary)
                        .foregroundColor(.primary)
                        .cornerRadius(50)
                }

                Button(action: {
                    puzzleEngine.initializeRandomGame()
                    isGameViewActive = true
                }) {
                    Text("Start Random Game")
                        .bold()
                        .foregroundColor(.primary)
                        .frame(width: 200, height: 50, alignment: .center)
                        .background(Color.secondary)
                        .foregroundColor(.primary)
                        .cornerRadius(50)
                }

                Button(action: {
                    isSavedGamesViewActive = true
                }) {
                    Text("View Saved Games")
                        .bold()
                        .foregroundColor(.primary)
                        .frame(width: 200, height: 50, alignment: .center)
                        .background(Color.secondary)
                        .foregroundColor(.primary)
                        .cornerRadius(50)
                }
            }
        }
    }
}
0

There are 0 best solutions below