2

All of the examples on the Internet show the following type of code for implementing pickers:

struct MyView: View { var colours = ["Red", "Green", "Blue"] @State private var myColourIndex = 1 @State private var myColour = "Green" var body: some View { VStack { Picker(selection: $myColourIndex, label: Text("Choose a colour")) { ForEach(0 ..< colours.count) { Text(self.colours[$0]) } } } } } 

This is probably very obvious but my question is how do you set the section based on the string value stored in myColour rather than the index?

1 Answer 1

3

Actually, almost the same (tested & works with Xcode 11.2 / iOS 13.2):

struct MyView: View { var colours = ["Red", "Green", "Blue"] @State private var myColourIndex = 1 @State private var myColour = "Green" var body: some View { VStack { Picker("Choose a colour", selection: $myColour) { ForEach(colours, id: \.self) { colour in Text(colour) } } } } } 
Sign up to request clarification or add additional context in comments.

1 Comment

Is there a way to programmatically close the picker without using the back button?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.