|
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:
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:
In your code stub below, your intention is to show and collect the User's Settings. How is this done exactly?
@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:
See --> Cup Cake Corner |
|
King asks:
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.
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:
|
|
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! |
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!
Sponsor Hacking with Swift and reach the world's largest Swift community!
You need to create an account or log in to reply.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.