How can I press a button from one view and activate sth in another view?

Hello,
I have created a view called Graph where a graph is displayed when users press a button in another view called Play. The Graph view is inside the Play view and I'm wondering what I have to do to get this to work properly.

in PLAY view:

struct Play: View {
var body: some View {
 
          ViewGraph()
             
      }
}
Sorry, bug your description and code are too vague and I do not understand what you really want to do?

when users press a button in another view called Play

In your code, there is no button in the view Play.

I have created a view called Graph 

Your Play has a view named ViewGraph inside, is it the same as the view called Graph?

a graph is displayed

What triggers the graph to be displayed? Can you show the whole code of Graph (or ViewGraph) ?



What is your problem ?
Is it passing data between views ?
If so, you can use @Binding & @State or us environment var.
Hi,
sorry. What I wanted to say is if it is possible to use @binding properties with a Bool var because I want to press a button from one view and activate the animation in viewgraph() (which is a content inside Play).

-------------------

struct Play: View {

 var body: some View {

    ...

        if Graph {
           
          ViewGraph()
           
        }
...

}  .toolbar {
      ToolbarItem(placement: .primaryAction) {

          Menu{
            Section {
              Button(action: {

WHEN I PRESS THIS BUTTON, I WANT THE GRAPH TO START

              }, label: {
                Text("Start recording")
              })
              Button(action: {
     WHEN I PRESS THIS BUTTON, I WANT THE GRAPH TO STOP
              }, label: {
                Text("Stop recording")
              })
            }
             
          }
          label: {
            Label("Add", systemImage: "playpause")
          }

------------------------------

struct ViewGraph: View {
   
  
  //  graph 1
  struct LineGraph: Shape {
     
    var dataPoints: [CGFloat]
     
    func path(in rect: CGRect) -> Path {
      func point(at ix: Int) -> CGPoint {
        let point = dataPoints[ix]
        let x = rect.width * CGFloat(ix) / CGFloat(dataPoints.count - 1)
        let y = (1-point) * rect.height
        return CGPoint(x: x, y: y)
      }
       
      return Path { p in
        guard dataPoints.count > 1 else { return }
        let start = dataPoints[0]
        p.move(to: CGPoint(x: 0, y: (1-start) * rect.height))
        for idx in dataPoints.indices {
          p.addLine(to: point(at: idx))
        }
      }
    }
  }
   
//  variables graph
   
  @State public var on = false 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 {
     
 
      ZStack{
         
        Image("backGraph")
          .resizable()
          .aspectRatio(contentMode: .fit)
         
        VStack{

              LineGraph(dataPoints: sampleDataC)
                .trim(to: on ? 1 : 0)
                .stroke(ColorConG, lineWidth: 1.5)
                .aspectRatio(16/1, contentMode: .fit)
                .frame(width: 320, height: 20, alignment: .center)
                .offset(x: 5, y: 25.0)
                .id(viewID)
       }

      Button("Animate") {
        if !on{
        withAnimation(.linear(duration: 130)) {
          self.on.toggle()
           
          }
        }
        else{
          self.on.toggle()
          viewID += 1
        }
      }
    }
  }
}

My idea is to create the button Animate in the other view (Play) inside the toolbar.
My idea was to use a @Binding property but I couldn't get it.
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.swift
Code 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.)


How can I press a button from one view and activate sth in another view?
 
 
Q