I think I'm close but also missing something fundamental here with SwiftUI and passing data.
- I have a top-level Color var called "masterColor" which I house in my "DataModel" struct.
- Then in my view "NightLight", I have a system "ColorPicker", where I use a local var "localColor" to reflects whatever value the ColorPicker has.
- Finally I have a "BackgroundControllerView", which sets a background color (which I would like to read the dataModel.masterColor)
What I'm trying to do set the dataModel.masterColor (which my whole app can see) equal to my localColor, which only NightLight can see. I've simplified the structure here a bit but the thrust of the question is about how to take local data and set something global equal to it for the rest of the app to see.
struct DataModel {
@State var masterColor = Color.red
}
struct NightLight: View {
@Binding var dataModel: DataModel
@State var localColor = Color.blue
var body: some View {
ColorPicker("Pick Color", selection: $localColor)
// Question: somehow set dataModel.masterColor = localColor ??
}
}
struct BackgroundControllerView: View {
@Binding var dataModel: DataModel
var body: some View {
Rectangle()
.fill(dataModel.masterColor)
}
}
Very much appreciate any help!