In this example we add a Button to the Main View which changes show Variable.
And we add Text to the Sub View which changes color based on isPresented.
When we press this Button it changes show Variable.
But since show and isPresented can be considered the same Variables this also changes isPresented Variable.
Which in turn changes color of the Text in the Sub View which depends on isPresented Variable.
ContentView.swift
struct ContentView : View {
@State var show = true
var body:some View {
VStack(spacing: 20) {
Text("MAIN VIEW").foregroundColor(show ? .blue : .red)
Button("CHANGE") { self.show.toggle() }
ToggleButton(isPresented: self.$show)
}.padding().border(Color.blue, width: 3)
}
}
struct ToggleButton: View {
@Binding var isPresented: Bool
var body: some View {
VStack(spacing: 20) {
Text("SUB VIEW").foregroundColor(isPresented ? .blue : .red)
Button("TOGGLE") { self.isPresented.toggle() }
}.padding().border(Color.red, width: 3)
}
}