Automate Your Builds and Testing with Bitrise
Using a UIPickerView as input for another control
August 30, 2019 · 1 min read
You want to offer a pre-selected list of options to a user. This helps protect the content that gets entered into another control like a label or a text field or maybe even just an entry to be stored in a database.
So the solution is actually very straight forward, give them a picker that they can scroll to find the desired selection in. As that value changes, you can take that selection and put it somewhere else in your application. In this example, we change the value of a label.
- Place the UIPickerView and Label on the storyboard.
- Make the view controller the delegate and data source for the UIPickerView.
- Add the needed protocols for acting as a delegate.
- Add an outlet for the label so we can access it’s value in code.
- Provide data to the UIPickerView.
- Make use of the
pickerView:didSelectRow:inComponent:to detect changes and update the label.