1

I'm building a "Charade-like-game" app with five screens and a simple GameModel class consisting of five objects, including teams, round, and scores.

Screen1 dynamically determines the number of teams playing via @StateObject.

Screen2 is a scoreboard that displays in a VStack the dynamic number of teams playing via @ObservedObject, alongside scores from two rounds of play.

Screen3 shows the Charade criteria, and Screen4 is a timer. Neither of these affect the GameModel.

Screen5 gives the other teams a chance to score the performance. That value is then passed back to the Screen2 scoreboard.

The navigation flow begins at 1 then moves to 2 > 3 > 4 > 5 then back to 2 to repeat the cycle until the users close the app, like this:

enter image description here

I got tangled up in the navigation because I wanted the back button on Screen2 to start the game over, rather than return to Screen5. When I used @Environment(\.presentationMode) and presentationMode.wrappedValue.dismiss(), it didn't navigate properly.

Therefore I tried building the navigation programmatically using a @State Boolean, to change the screen like this:

if playGamePressed {
    Screen2View()
} else {
    Screen1 code... 

    Select # of teams binded via @StateObject (my source of truth)

    Button(action: {
        playGamePressed = true
    } 
}

Now, the @StateObject binding no longer passes the dynamic number of teams. Everything worked prior to changing the navigation to programmatic.

I used this as a reference: Swiftui nested navigation issue while using navigation link, but the solution was .presentationMode that didn't work for me.

What am I missing?

ScottyA
  • 192
  • 1
  • 12

0 Answers0