How To Get Selected Value From UIPickerView
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
UIPickerView is a UIKit component that presents a spinning-wheel interface for selecting from a set of options. To get the selected value, you call selectedRow(inComponent:) on the picker view and use that index to look up the value in your data source array. The picker view uses a delegate/data source pattern — you provide the data through UIPickerViewDataSource and respond to selections through UIPickerViewDelegate. There is no .selectedValue property; you must map the selected row index back to your data model.
Basic Setup
Getting the Selected Value at Any Time
Multi-Component Picker
Setting a Default Selection
Using UIPickerView with UITextField
A common pattern is presenting the picker as the input view for a text field.
Common Pitfalls
- Calling
selectedRowbefore the picker is displayed:selectedRow(inComponent:)returns 0 (first row) before the user interacts with the picker. If 0 is not a valid default, callselectRow(_:inComponent:animated:)inviewDidLoadto set an explicit default. - Forgetting that
selectRowdoes not trigger the delegate: Programmatically callingselectRow(_:inComponent:animated:)does not calldidSelectRow. If you need to update UI or state when setting a default, call your update logic manually afterselectRow. - Index out of range when data source changes: If you update the data source array (e.g., filter options) without reloading the picker, the selected row index may exceed the new array bounds. Call
pickerView.reloadAllComponents()after changing data, then validate the selected index. - Not setting both delegate and dataSource:
UIPickerViewrequires bothdelegateanddataSourceto be set. Missing either results in an empty picker or crashes. Both protocols must be adopted by the same or different objects. - Using
UIDatePickerinstead ofUIPickerViewfor dates: For date/time selection, useUIDatePickerwhich provides a native date interface and returns aDateobject directly via.dateproperty. Only useUIPickerViewfor custom non-date option lists.
Summary
- Call
pickerView.selectedRow(inComponent:)to get the selected row index, then look up the value in your data array - Implement both
UIPickerViewDelegateandUIPickerViewDataSourceprotocols - Use
didSelectRowdelegate method to respond to user selections in real time - Use
selectRow(_:inComponent:animated:)to set a default — but call update logic manually since it does not trigger the delegate - Present the picker as
textField.inputViewfor form-style interfaces
Related reading
- How to get Spinner value?
- How to get text in a CATextLayer to be clear
- How to get the absolute coordinates of a view
- How to get the ActionBar height?
- How to get the Android device's primary e-mail address
- How to get the current index in for each Kotlin
- How to get the device's IMEI/ESN programmatically in android?
- How to get the filename from the filepath in swift
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.