NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

Edutainment Challenge: Passing Data between Views

Forums > 100 Days of SwiftUI

I'm trying to approach this project by creating two custom views: one for the settings and the other for the game. That being said, I've got to juggle between ContentView, Game, and Settings, and I don't know how to pass the data back and forth between these (to do things like keep a score, etc.). I've got a State variable in my ContentView that keeps track of a score, and I'd like for Game to be able to add to that, and I want Settings to be able to set it back to zero. How would I go about doing this?


Without giving too much away, have a look at this article from Paul. I would say using EnvironmentObject would best suit your circumstances and this article will explain to you what to do.



edutainment is challenge right before introduction of switching between views. so i think it should be done using everything you learned before day 36.


This answer might be coming in a bit late, $ essentiall means it's a Binding that wraps around a certain value.

   get: { return x }
   set: { x = $0 }

by definition, we can then pass these values as Binding<Type> and store them. This will then create a 2 way binding on every view holding this value.


Hacking with Swift is sponsored by Judo

SPONSORED Let’s face it, SwiftUI previews are limited, slow, and painful. Judo takes a different approach to building visually—think Interface Builder for SwiftUI. Build your interface in a completely visual canvas, then drag and drop into your Xcode project and wire up button clicks to custom code. Download the Mac App and start your free trial today!

Try now

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

Archived topic

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.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.