iOS中如何封装UIPickerView

发布时间:2021-09-28 10:14:11 作者:小新
来源:亿速云 阅读:132

这篇文章主要介绍iOS中如何封装UIPickerView,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

所需属性

/** pickerView*/@property (nonatomic, strong) UIPickerView pickerView;/* pickerView背景*/@property (nonatomic, strong) UIView pickerBackGroundView;/* 背景*/@property (nonatomic, strong) UIView backGroundView;/* 确认按钮*/@property (nonatomic, strong) UIButton sureButton;/* 取消按钮*/@property (nonatomic, strong) UIButton cancelButton;/* 单列pickerView*/@property (nonatomic, strong) NSMutableArray slDataArray;/* 双列pickerView*/@property (nonatomic, strong) NSMutableArray *mulDataArray;

如果只需要一列的话,只需要传入一个数据数组:slDataArray,如果需要两行,则两个数组都需要赋值。

实现UIPickerView代理方法

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

这里根据两个数组来初始化pickerView的内容,即判断第二个数组(mulDataArray)是否有数据,有数据的话代表加载两列的pickerView,否则加载一列。

功能实现

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

第一个方法是只有一列的pickerView初始化是让其选中哪行,第二个则是两列的选择方法。

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

这个方法是显示或者隐藏pickerView,通过动画的方式,背景慢慢变黑或者透明,pickerView从下往上出现或者从上往下消失。

-(void)pickerViewReloadData{ [self.pickerView reloadAllComponents];}

刷新pickerView数据,加载另一个pickerView时,调用该方法刷新。

以上是“iOS中如何封装UIPickerView”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

推荐阅读:
  1. UIPickerView 模块示例demo
  2. 代码实现UIPickerView

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

ios uipickerview

上一篇:Windows x64 如何安装Oracle 11g第二版

下一篇:.net framework中Windows Forms如何创建功能区应用程序

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》