- Picker() is an ideal component for any situation where a user needs to select options.
- Different styles and formats can be applied, each better suited to either large or smaller data sets.
1. Write a default picker code
Picker(
"Text",
selection: .constant(1),
content: {
Text("1").tag(1)
})
2. Bind the string into selection
@State var selection: String = "1"
- change the tag number into string
Text("1").tag("1")...
- Create Text with Age and selected value in HStack
HStack{
Text("Age:")
Text(selection)
}
3. Use ForEach instead of writing one by one.
ForEach(18..<100) { number in
Text("\(number)").tag("\(number)")
}
- Now picker has a number from 18 to 99