When you want to give the user option to select some value from the available options then one of the best options is using Picker.
First, you need State property to keep the selected value.
@State private var number = 1
Next, create VStack with Text that will show the current value of the number.
VStack {
Text(“Numer: \(number)“)
}
The last step is to create a Picker view that needs to have some text, selection that binds what the user selected, and content that will have a view to select.
Picker(“”, selection: $number) {
ForEach(1…10, id: \.self) {
Text(“\($0)“)
}
}
There is ForEach that iterate from 1 to 10 and each time the user stops scrolling on some value then it’ll be put into number state property.
Instead of numbers, there can be an array of letters as well.
@State private var letter = “b”
var body: some View {
VStack {
Text(“Letter: \(letter)“)
Picker(“”, selection: $letter) {
ForEach([“a”,“b”,“c”], id: \.self) {
Text(“\($0)“)
}
}
}
}