TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

SOLVED: Day35 - Can't find a way two handle the View switch using closures

Forums > 100 Days of SwiftUI

In the day 35 challenge I would like to understand how it works to switch between the views (GameView,SettingsView) using closures. I tried a lot but without success and it would be great if someone can give me a hint. I tried to understand the hint given by @FlyOstrich at SOLVED: Looking for clue for Day 35 challenge (Multiply!) but without success.
It would be great if someone can give me a hint on how to pass the Bool gameStarted between the ContentView and e.g. SettingsView/GameView to be able to use this within the Zstack to show either SettingsView or GameView.

Any ideas are welcome :-)

Regards, Ralf

3      

First need a Bool to track which View is showning

 @State private var isGameStarted = false

Then in the ContentView you can have, which will show either view depend on isGameStarted

var body: some View {
    if isGameStarted {
        GameView()
    } else {
        SettingsView()
    }
}

now need to be able to change between so add a method

func startGame() {
    isGameStarted = true
}

but what needed is say number of questions to answer as a Int

@State private var numberQuestions = 0

need to be able set numberQuestions so change the method

func startGame(with numberQuestions: Int) {
    self.numberQuestions = numberQuestions
    isGameStarted = true
}

and now can pass the numberQuestions to the GameView

var body: some View {
    if isGameStarted {
        GameView(numberQuestions: numberQuestions)
    } else {
        SettingsView()
    }
}

so now we need to be able to call the startGame method this method take an Int and return nothing which (Int) -> Void so in the SettingView we need to add let startGame: (Int) -> Void and then you can do a Button with action to start the game

struct SettingsView: View {
    @State private var numberQuestions = 0
    let startGame: (Int) -> Void

    var body: some View {
        VStack {
            HStack {
                Text("Number of Questions")
                Picker("Number of Questions", selection: $numberQuestions) {
                    ForEach(1..<50) {
                        Text("\($0)")
                    }
                }
            }

            Button("Start Game") {
                startGame(numberQuestions + 1)
            }
            .buttonStyle(.borderedProminent)
        }
    }
}

This is very rough as just want to show how to pass the method from one view to another (took me age to get this). It might give you a starting point for the challange.

struct ContentView: View {
    @State private var isGameStarted = false
    @State private var numberQuestions = 0

    var body: some View {
        if isGameStarted {
            GameView(numberQuestions: numberQuestions)
        } else {
            SettingsView(startGame: startGame)
        }
    }

    func startGame(with numberQuestions: Int) {
        self.numberQuestions = numberQuestions
        isGameStarted = true
    }
}
struct GameView: View {
    let numberQuestions: Int

    var body: some View {
        Text("Game playing \(numberQuestions) questions")
    }
}
struct SettingsView: View {
    @State private var numberQuestions = 0
    let startGame: (Int) -> Void

    var body: some View {
        VStack {
            HStack {
                Text("Number of Questions")
                Picker("Number of Questions", selection: $numberQuestions) {
                    ForEach(1..<50) {
                        Text("\($0)")
                    }
                }
            }

            Button("Start Game") {
                startGame(numberQuestions + 1)
            }
            .buttonStyle(.borderedProminent)
        }
    }
}

4      

Hello @NigelGee,

Thank you! You definitely made my day :-)
I've been sitting on the problem for 2 days, searching the web, browsing the previous days of HWS 100days SwiftUI, but not really finding an approach. Even though I'm glad to have a good, and for me also understandable approach now, I'm still very frustrated that I haven't made it myself even after 35 days of SwiftUI course at HWS. However, I will not be discouraged and continue to work on the next videos and challenges of Paul, because the course is really excellent and it makes me happy and brave that there is such good help from the forum. In this sense, MANY THANKS again and continue to have a nice day.

Greetings Ralf

2      

I used some of these ideas in my version of the Day 35 challenge. I have it working well, except for when the game is over, I dont know how to get it to go back to SettingsView(). Going from SettingsView() to GameView() works and the game play works, I just cant figure out how to go backwards?

setting gameStarted = false does nothing, and I tried a NavigationLink but I am not sure I did that correctly as that didnt work either...

2      

I actually figured this out...

2      

Just to say you helped me alot with this post!

thank you!!

   

Hacking with Swift is sponsored by Superwall.

SPONSORED Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls – free of charge.

Learn More

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.