Good afternoon!
Tell me how to correctly implement a connection between different views for general object changes?
For example, there is 1 view where I use the button to change the background color, go to view 2 and there is already a changed background color and vice versa with view 2 change the color for all other views?
I tried to use "ObservableObject" but I get to change the color on one view, when I switch, the standard is restored in the next view
View connection
Button
Background color
Tell me how to correctly implement a connection between different views for general object changes?
For example, there is 1 view where I use the button to change the background color, go to view 2 and there is already a changed background color and vice versa with view 2 change the color for all other views?
I tried to use "ObservableObject" but I get to change the color on one view, when I switch, the standard is restored in the next view
Code Block class Store: ObservableObject { @Published var colorBackground = "Black" }
View connection
Code Block @ObservedObject var store: Store
Button
Code Block Button(action: { self.store.colorBackground = "Black" }, label: { })
Background color
Code Block ZStack { Color(store.colorBackground) .edgesIgnoringSafeArea(.all) }
@Tact,
There wasn't much provided except for a few pieces of code, so it is hard to determine the exact flow of your application, so I took some liberty in piecing together what you might have. I have also never tried to implement this style of application, so I decided to try my best to solve it; learned some new things! Below is what I got working:
I ran into the issue that others have encountered when trying to set the background color that involves the NavigationView. It appears that the the underline View either doesn't have/use the dimensions of the screen, so either using GeometryReader or .infinity should get a desired effect of turning the whole background color via just SwiftUI. Hopefully this will get you going and understand the relationship on how to pass values around different views.
If you don't plan on using NavigationView or some of the other views that have issues with .background, you can easily use Color() example you provided.
There wasn't much provided except for a few pieces of code, so it is hard to determine the exact flow of your application, so I took some liberty in piecing together what you might have. I have also never tried to implement this style of application, so I decided to try my best to solve it; learned some new things! Below is what I got working:
Code Block class Store: ObservableObject { @Published var backgroundColor: Color init() { self.backgroundColor = .green } } struct ChangeBackgroundColor: View { @Binding var backgroundColor: Color var body: some View { ZStack { VStack { Button(action: { self.backgroundColor = .black }, label: { Text("Black") }) Button(action: { self.backgroundColor = .green }, label: { Text("Green") }) } } .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity) .background(backgroundColor).edgesIgnoringSafeArea(.all) } } struct ContentView: View { @ObservedObject var store: Store = Store() var body: some View { NavigationView { VStack { NavigationLink(destination: ChangeBackgroundColor(backgroundColor: $store.backgroundColor)) { Text("Pick Background Color") } } .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity) .background(store.backgroundColor).edgesIgnoringSafeArea(.all) } } }
I ran into the issue that others have encountered when trying to set the background color that involves the NavigationView. It appears that the the underline View either doesn't have/use the dimensions of the screen, so either using GeometryReader or .infinity should get a desired effect of turning the whole background color via just SwiftUI. Hopefully this will get you going and understand the relationship on how to pass values around different views.
If you don't plan on using NavigationView or some of the other views that have issues with .background, you can easily use Color() example you provided.