SoFunction
Updated on 2025-04-12

Simple encapsulation example of iOS UIPickerView

Preface

In actual iOS projects, there are often multiple places in the interface that need to use UIPickerView. If you create a UIPickerView in each place you need to use it, it will not only consume performance, but also make your code more messy and redundant. Therefore, I will introduce to you some simple encapsulations of UIPickerView here.

Required properties

/** pickerView*/
@property (nonatomic, strong) UIPickerView pickerView;
/* pickerView background*/
@property (nonatomic, strong) UIView pickerBackGroundView;
/* background*/
@property (nonatomic, strong) UIView backGroundView;
/* Confirm button*/
@property (nonatomic, strong) UIButton sureButton;
/* Cancel button*/
@property (nonatomic, strong) UIButton cancelButton;
/* Single column pickerView*/
@property (nonatomic, strong) NSMutableArray slDataArray;
/* Double column pickerView*/
@property (nonatomic, strong) NSMutableArray *mulDataArray;

If only one column is needed, you only need to pass in a data array: slDataArray. If two rows are needed, both arrays need to be assigned.

Implement UIPickerView proxy method

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
  if ( == 0) {
  return 1;
 }else {
  return 2;
  }
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
 if (component == 0) {
  return ;
  }else {
  return ;
 }
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
 if (component == 0) {
  return [row];
 }else {
 return [row];
 }
}

Here, the content of the pickerView is initialized based on the two arrays, that is, whether the second array (mulDataArray) has data. If there is data, it means loading the pickerView of the two columns, otherwise one column will be loaded.

Functional implementation

-(void)pickerViewSelectRow:(NSInteger)row
{
  = row;
 [ selectRow:row inComponent:0 animated:NO];
}
-(void)pickerViewSelectRow:(NSInteger)row lastRow:(NSInteger)lastRow{
 [ selectRow:row inComponent:0 animated:NO];
 [ selectRow:lastRow inComponent:1 animated:NO];
}

The first method is to initialize the pickerView with only one column to select which row it is, and the second method is to select the two columns.

-(void)showOrHidePickerView:(BOOL)isShow{
 if (isShow) {
  if ( == NO) {
   [self addSubview:];
   [self addSubview:];
   [UIView animateWithDuration:0.3 animations:^{
      = 0.5;
      = CGRectMake(0, SCREEN_HEIGHT -220, SCREEN_WIDTH, 220);
   } completion:^(BOOL finished) {    = YES;
   }];
  }
 }else {
  if () {
   [UIView animateWithDuration:0.3 animations:^{
    = 0.0;
    = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 220);
  } completion:^(BOOL finished) {
   [ removeFromSuperview];
   [ removeFromSuperview];
    = NO;
  }];
  }
 }
}

This method is to display or hide the pickerView. Through animation, the background slowly becomes dark or transparent, and the pickerView appears from bottom to top or disappears from top to bottom.

-(void)pickerViewReloadData{
 [ reloadAllComponents];
}

Refresh pickerView data, and when another pickerView is loaded, the method is called to refresh.

Summarize

The above is the entire content of this article. I hope that the content of this article has certain reference value for your study or work. Thank you for your support.