Thanks for showing additional code. But please use the
Code block feature shown as
< > at the bottom bar of the editing area.
(And please be more punctual about identifiers. Identifiers in Swift are case-sensitive and a slight difference may confuse readers.)
if it is possible to use @binding properties with a Bool var
Of course you can. When you want to share a Bool var between views, you declare an
@State var in the parent view and pass the Binding of it to the child view.
Play.swift:
Code Block | struct Play: View { |
| @State var graphIsOn: Bool = false |
| @State var showsGraph: Bool = true |
|
| var body: some View { |
| VStack { |
| //... |
| if showsGraph { |
| ViewGraph(isOn: $graphIsOn) //Pass Binding to child view |
| } |
| //... |
| } |
| .toolbar { |
| ToolbarItem(placement: .primaryAction) { |
| Menu{ |
| Section { |
| Button(action: { |
| //WHEN I PRESS THIS BUTTON, I WANT THE GRAPH TO START |
| graphIsOn = true |
| }, label: { |
| Text("Start recording") |
| }) |
| Button(action: { |
| //WHEN I PRESS THIS BUTTON, I WANT THE GRAPH TO STOP |
| graphIsOn = false |
| }, label: { |
| Text("Stop recording") |
| }) |
| } |
| } |
| label: { |
| Label("Add", systemImage: "playpause") |
| } |
| } |
| } |
| } |
| } |
ViewGraph.swiftCode Block | struct ViewGraph: View { |
| |
| //... |
|
| // variables graph |
| |
| @Binding public var isOn: Bool //This is the var which starts the graph |
| |
| let sampleDataC: [CGFloat] = [0.1, 0.2, 0.45, 0.6, -0.8, -1.1, -0.4, 0.1, 0.2, 0.45, 0.6, -0.8, -0.2, 0.5, 0.3] |
| |
| @State private var colorConG = Color.white |
| |
| @State private var viewID = 0 |
| |
| var body: some View { |
| //... Use `isOn` as your `on`... |
| } |
| } |
(I renamed
on to
isOn, as
on is too short to be meaningful.)