Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to encapsulate UIPickerView in iOS

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces how to encapsulate UIPickerView in iOS, which is very detailed and has certain reference value. Friends who are interested must finish it!

Required attribute

/ * * 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 you need only one column, you only need to pass in one data array: slDataArray, and if you need two rows, both arrays need to be assigned.

Implementing the UIPickerView proxy method

-(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];}}

Here, the contents of pickerView are initialized according to two arrays, that is, to determine whether the second array (mulDataArray) has data. If there is data, it means to load two columns of pickerView, otherwise load one column.

Function realization

-(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];}

The first method is that only one-column pickerView initializes which row to select, and the second is a two-column selection method.

-(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; self.pickerBackGroundView.frame = CGRectMake (0, SCREEN_HEIGHT, SCREEN_WIDTH, 220);} completion: ^ (BOOL finished) {[self.backGroundView removeFromSuperview]; [self.pickerBackGroundView removeFromSuperview]; self.isPickerShow = NO;}];}

This method is to show or hide pickerView, through animation, the background slowly darkens or transparent, pickerView appears from the bottom up or disappears from the top down.

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

Refresh the pickerView data and call this method to refresh when another pickerView is loaded.

The above is all the contents of the article "how to encapsulate UIPickerView in iOS". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report