Implementing UIPickerView and UIDatePicker in iOS
UIPickerView Properties
Key properties for configuring a UIPickerView include:
// Specifies the data source object that provides data for the picker
@property(nonatomic, weak) id<UIPickerViewDataSource> dataSource;
// Specifies the delegate object that handles display and selection events
@property(nonatomic, weak) id<UIPickerViewDelegate> delegate;
// Controls visibility of the selection indicator overlay
@property(nonatomic) BOOL showsSelectionIndicator;
// Read-only property indicating total number of components (columns)
@property(nonatomic, readonly) NSInteger numberOfComponents;
UIPickerView Methods
Essential methods for managing picker view state:
// Reloads all components with fresh data
- (void)refreshAllComponents;
// Reloads data for a specific component
- (void)refreshComponent:(NSInteger)componentIndex;
// Programmatically selects a row in a component
- (void)selectRowAtIndex:(NSInteger)rowIndex
inComponent:(NSInteger)componentIndex
anmiated:(BOOL)shouldAnimate;
// Returns currently selected row index for specified component
- (NSInteger)currentSelectedRowInComponent:(NSInteger)componentIndex;
Data Source Protocol Methods
Required methods for providing data to UIPickerView:
// Returns number of columns (components) in picker
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)picker;
// Returns number of rows for specified component
- (NSInteger)pickerView:(UIPickerView *)picker
numberOfRowsInComponent:(NSInteger)componentIndex;
Delegate Protocol Methods
Methods for customizing appearance and handling interactions:
// Defines width for each component
- (CGFloat)pickerView:(UIPickerView *)picker
widthForComponent:(NSInteger)componentIndex;
// Defines row height for each component
- (CGFloat)pickerView:(UIPickerView *)picker
rowHeightForComponent:(NSInteger)componentIndex;
// Provides text content for specific row
- (NSString *)pickerView:(UIPickerView *)picker
titleForRow:(NSInteger)rowIndex
forComponent:(NSInteger)componentIndex;
// Provides custom view for specific row (alternative too text)
- (UIView *)pickerView:(UIPickerView *)picker
viewForRow:(NSInteger)rowIndex
forComponent:(NSInteger)componentIndex
reuseView:(UIView *)existingView;
// Called when user selects a row
- (void)pickerView:(UIPickerView *)picker
didSelectRow:(NSInteger)rowIndex
inComponent:(NSInteger)componentIndex;
UIDatePicker Configuration
Core properties for date picker customization:
// Sets the display mode (date, time, dateAndTime, countDownTimer)
@property (nonatomic) UIDatePickerMode selectionMode;
// Sets locale to regional formatting
@property (nonatomic, strong) NSLocale *regionalLocale;
Handling UIDatePicker Events
Since UIDatePicker inherits from UIControl, use target-action pattern for event handling:
// Example: Adding target for value changes
[datePickerInstance addTarget:self
action:@selector(dateSelectionChanged:)
forControlEvents:UIControlEventValueChanged];
// Handler method implementation
- (void)dateSelectionChanged:(UIDatePicker *)sender {
NSDate *selectedDate = sender.date;
// Process selected date
}