|
I am working on the day 35 challenge of 100 days of SwiftUI but I don't know how to proceed. The instructions say "Try and break up your layouts into new SwiftUI views, rather than putting everything in ContentView." But when I try to create separate Views for "Setting Selection", "Gameplay", and "Game Over", I run into problems with accessing the @State private vars that I created in Content View. For example, in ContentView I am trying to use "@State private var settingsSelected: Bool" to determine whether the "SelectSettingsView" or the "GameplayView" should be displayed. However, I don't know how to give ContentView access to read the settingsSelected variable, while also giving SelectSettingsView a button that would be able to toggle settingsSelected to True so ContentView could read it. I may have missed something in the previous lessons, but it doesn't seem to me that this has been covered in the previous course material yet. This is the code that I have so far, and it is in the definition of "struct SettingSelectionView: View" that I am getting errors.
|
|
You would pass the parameters into the Also the constants maxDifficulty and numQuestionsOptions need to be passed from your main ContentView to the SettingSelectionView struct.
The in the main ContentView
|
|
I'll try it tomorrow, but it looks like that will probably work for me. I just looked up |
|
An alternative would be to use a function rather than a structure. The function would return 'some View' and reside inside the main ContentView structure. That doesn't involve As there is only one item in the function, you could omit the
|
|
Thank you, I tried using struct and binding but its not working, so I use function instead. |
SPONSORED Get accurate app localizations in minutes using AI. Choose your languages & receive translations for 40+ markets!
Sponsor Hacking with Swift and reach the world's largest Swift community!
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.