WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

Day 35 - Questions on passing data between views (Edutainment app)

Forums > 100 Days of SwiftUI

I've implemented my own solution by putting everything inside one ContentView, then I read the tips and think I should try breaking them up.

However, my concept of passing data between views is unclear, and have no idea what the below means:

... in the meantime send data using closures – the button action from your settings view would call a function passed in by the parent view that starts the game with the user’s settings, for example.

My understanding is do to something like this:

// Create custom views for the container (parent), and the form and button (children) respectively
struct SettingsPanel: View {
// Have local states?
var baseNumber: Int
var numberOfQuestions: Int

  UsersSettings {

  }

  GameStartButton {
  }
}

struct UsersSettings: View {
// Takes user's input and store the values in parent's variables?
}

struct GameStartButton: View {
// Runs a local function on press which takes the value of the settings from its parent using closure?
}

Can anyone show me an example of how it's done exactly? What if the values of the variables need to be used by another unrelated view (i.e. not a parent/child of the SettingsPanel)?

Thanks a lot in advance.

   

King asks:

Can anyone show me an example of how it's done exactly?

In your code stub below, your intention is to show and collect the User's Settings. How is this done exactly?

struct UsersSettings: View {
// Takes user's input and store the values in parent's View Model?
}

@twostraws details the steps for you in the CupCakeCorner application! Have you completed these lessons? If so, do your self a favor and repeat all of these lessons, keeping your question in mind, How can you take user's input and store the values in parent's view model?

Big Picture:

  1. You need to design a model to hold the data you want.
  2. You need to create this model in some view. (Please don't call it ContentView.) Views primarily show what's in a model.
  3. You pass the model, or part of the model to subviews (UserSettingsView) to display or collect data. When data in the subview is updated, the model is updated.

See --> Cup Cake Corner

   

King asks:

Can anyone show me an example of how it's done exactly?

In your code stub below, your intention is to clear all the scores, shuffle a deck, generate some random game settings and start a new game. Probably.


struct GameStartButton: View {
// Runs a local function on press which takes the value of the settings from its parent using closure?  <-- NO
     myGameObject.startNewGame()  // Tell the game object what you want it to do.
     // Keep the game logic out of the view. Put it in the game model.
}

SwiftUI is a declarative architecture. Your buttons probably should DECLARE what you want to happen. Consider NOT putting code, if statements and other logic in your button's action.

You probably DO NOT want a local function to handle the detailed business of starting a new game. Instead, this button should tell the ViewModel that the user wants to start a new game. Let the view just show what's in the model. Let the ViewModel orchestrate changes to the game model and the view.

Big Picture:

  1. Your game model holds the details of your game. How many questions, deck of cards, number of dice, player score, etc.
  2. Your game model has a function to reset the game to a starting point. Reset score. Reshuffle the deck. Deal first 3 cards.
  3. Your view represents what is in your model.
  4. Your view has buttons to change the model. But the buttons send method calls to the model, allowing the model to run game specific analysis, update scores, update next available pieces, toss in random trolls, or dragons, etc. When the model is updated, the GameView should re-draw itself.
  5. Keep your game logic out of buttons. Focus button actions on calling methods in your game's model.

   

Thanks @Obelix for your detailed reply. Your points from the big picture perspective are especially helpful, but I'm not quite there yet and haven't reached the Cup Cake Corner project, so let me make sure I understand everything after completing more tutorials before asking further question.

Just that for this project (the Edutainment app - Randomly ask questions of multiplication of a chosen number x 1...12), it seems quite straightforward when I did it in the same way as the Guess the Flag project (everything in one view, and I didn't even store the questions and answers in an array). So I don't understand what I'm supposed to achieve by following the tips and how to do so - "... in the meantime send data using closures – the button action from your settings view would call a function passed in by the parent view that starts the game with the user’s settings"

I guess the "ViewModel" you mentioned is essential here and especially for larger project. Let me find out more and come back to this.

Thanks again!

   

Hacking with Swift is sponsored by Fernando Olivares

SPONSORED Fernando's book will guide you in fixing bugs in three real, open-source, downloadable apps from the App Store. Learn applied programming fundamentals by refactoring real code from published apps. Hacking with Swift readers get a $10 discount!

Read the book

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.