Hopefully you can see what I'm trying to achieve from the code below but simply put, I'm trying to update .selectedTown which is binded to my Picker. The row tapped on will bind to .selectedTown which will then update the Text 'Your selected town is: [.selectedTown]' However, the selected row is not binding and the text remains 'Your selected town is: '
struct ContentView: View { struct Town: Identifiable { let name: String let id = UUID() } private var towns = [ Town(name: "Bristol"), Town(name: "Oxford"), Town(name: "Portsmouth"), Town(name: "Newport"), Town(name: "Glasgow"), ] @State private var selectedTown: String = "" var body: some View { NavigationView { VStack { Form { Section { Picker("", selection: $selectedTown) { ForEach(towns, id: \.id) { Text("\($0.name)") } } .pickerStyle(.inline) .labelsHidden() } header: { Text("Random Towns") } } Text("Your selected town is: \(selectedTown)") .padding() } .navigationTitle("Random") } } }
Hopefully this is just a small fix but I've tried for what seems a day to find a solutino and am now stuck. Any help would be gratefully received,
Simon
