2 Views sharing Data: Picker and Variable Output

Hi have 2 Views. The PickerView and the PickerResultView.

I now want do place the Picker in one different View AND the OutputVariable also in one different view. What kind of wrapper do I need ?

PickerView


struct PickerView: View {

    @State var testnumber = "D123"

    var body: some View {

        Picker(selection: $testnumber,

               label: Text("PickerName"),

               content: {

            Text("John").tag("John")

            Text("John2").tag("John2")

            Text("John3").tag("John3")

        })

        .frame(width: 120,height: 100)

        .pickerStyle(.wheel)

    }
}
struct PickerView_Previews: PreviewProvider {

    static var previews: some View {

        PickerView()

    }

}

The PickerResultView:




struct PickerResultView: View {

   
@Binding var testnumber: String

    var body: some View {

        Text("\(testnumber)")
            .foregroundColor(.black)

        }
}
struct PickerResultView_Previews: PreviewProvider {

    static var previews: some View {

        PickerResultView(testnumber: .constant(""))
    }
}

Could you explain more precisely ?

  • Do you want to move the Picker form PickerView or create the same Picker in yet another View ?
  • What is variable Output? Where is it in code ?

The best would be to show the skeleton of code structure, with all the structs you want and which objects in them.

Note: environmentObject are a good solution to share data between multiple views.

2 Views sharing Data: Picker and Variable Output
 
 
Q