Print amount from single member instance on dynamic view

Hi, I am new here, and I hope that you guys will help me to find solution.

What I need:

  1. I have SingleMember View, which containt just
@State var text: String = ""
TextField("Amount", text:$text)

I` have MainView which contains

VStack{
ForEach(0..<3) {
SingleMember()
}
Button("Print all values"){
//action
}
}

What should I insert in button to get all three amount values?

First of all, you need a way for the SingleMember view to communicate to the MainView and vice versa – a two-way communication. You can achieve this with the @Binding property wrapper. It can be used like this:

struct SingleMember: View {
    @Binding var text: String

    var body: some View {
        TextField("Amount", text: $text)
    }
}


You then need to create a source-of-truth for those three values in the parent MainView that can also be passed to the child SingleMember views. You can either create three separate variables or an array of values depending on whether 3 is a fixed value or not. The binding can be created and passed around like this:

struct MainView: View {
    @State private var values: [String] = .init(repeating: "", count: 3)
    
    var body: some View {
        VStack {
            ForEach(0..<3) { i in
                SingleMember(text: $values[i])
            }

            Button("Print all values") {
                print(values)
            }
        }
    }
}

I solved, thanks, your reply get me in the right direction to solve my problem :)

Print amount from single member instance on dynamic view
 
 
Q