IOS custom date, time, city selector

Selector, I think we are not unfamiliar, when the user needs to choose a certain range of values in a fixed value, we will choose the way the selector. The selector can visually prompts the user to select a range of values, a unified message format, but also facilitate users to quickly choose, such as gender, under normal circumstances, only two kinds of men and women, then use a selector for users to choose, can avoid the wrong data input, but also more convenient for users to fill in. For example, users need access to information when using the birthday, the birthday can be unified mode selector format, if the user input word, birthday information format may appear various, is not conducive to the storage of data, but the selector way, users can find the corresponding date choice can be.

There is a kind of special UIPickerView selector in iOS, enter the UIPickerView header files, we can find the direct successor of the UIPickerView UIView, the event handling by proxy method, so when creating UIPickerView remember signing its agent UIPickerViewDataSource and UIPickerViewDelegate. In fact, it is a bit like UITbleView, but UIPickerView has more than a column attributes, UITbleView we are very familiar with, so you can compare the relevant attributes of UITbleView and proxy methods to learn and memory UIPickerView.

A.UIPickerView proxy method

(1) the proxy method corresponding to UIPickerViewDataSource (its proxy method must be implemented):

Returns the number of columns displayed

– (NSInteger) numberOfComponentsInPickerView: (UIPickerView *) pickerView;

Returns the number of rows to be displayed in each column

– (NSInteger) pickerView: (UIPickerView *) pickerView numberOfRowsInComponent: (NSInteger) component;

(2) the proxy method corresponding to UIPickerViewDelegate (its proxy method is selectively implemented according to requirements):

Returns the width of each column

– (CGFloat) pickerView: (UIPickerView *) pickerView widthForComponent: (NSInteger) component;

Returns the height of each column

– (CGFloat) pickerView: (UIPickerView *) pickerView rowHeightForComponent: (NSInteger) component;

Returns the contents of the list items to be displayed in the specified column in the UIPickerView control

(nullable NSString *) pickerView: (UIPickerView *) pickerView titleForRow: (NSInteger) row forComponent: (NSInteger) component;

Returns UIView as the display view of the specified row in the specified column in the UIPickerView control

(UIView *) pickerView: (UIPickerView *) pickerView viewForRow: (NSInteger) forComponent: row (NSInteger) component reusingView: (nullable UIView *);

Select the specified row in the specified column

– (void) pickerView: (UIPickerView *) pickerView didSelectRow: (NSInteger) row inComponent: (NSInteger) component;

Two, UIPickerView and UIDatePicker.

UIDatePicker is a system to help us package a good time and date selector, inherited in UIControl, UIDatePicker has some limitations, because it has only four display styles:

UIDatePickerModeTime, UIDatePickerModeDate, UIDatePickerModeDateAndTime, UIDatePickerModeCountDownTimer,

If the demand and these four styles are not met, then there is no way to use UIDatePicker, for example, when the only information display year, month, UIDatePicker obviously can’t satisfy our needs, then we can only through the UIPickerView from the definition you want selector.

Three, UIPickerView custom use

(1) create a base class inherited from UIView WXZBasePickView.

The style of our common selector is a view with a transparent background color, the bottom is the contents of the selector, there are OK and cancel button, as shown in figure:

IOS custom date, time, city selector
selector

So we create a base class view, the style of the view as shown in the diagram, and then create the selector based on the view based on the difference of the content.

Declare properties and methods in.H

#import < UIKit/UIKit.h> @interface; WXZBasePickView: UIView @property (nonatomic, strong) UIView *contentView; / / @property selector (nonatomic, strong) UIPickerView *pickerView; / / Cancel button @property (nonatomic, strong) UIButton *cancelButton; / / OK button @property (nonatomic, strong) UIButton *confirmButton; / / @property selector height of each column (nonatomic. Assign) CGFloat pickerViewHeight; / * * * create a view, view when the initial data initialization * / - (void) initPickView; / * * * button click event * / - (void) clickConfirmButton; / * * * * pickerView display - (void) show; / * * * * / - remove pickerView (void) disMiss; @end

Implementation of related methods in.M

#import "WXZBasePickView.h" #define ScreenWidth [UIScreen mainScreen].bounds.size.width #define ScreenHeight [UIScreen mainScreen].bounds.size.height @implementation WXZBasePickView (instancetype init) {self = [super init]; if (self) {_pickerViewHeight = 250; self.bounds = [UIScreen mainScreen].bounds [UIColor colorWithRed:0 green:0 blue:0; self.backgroundColor = alpha:0.64]; self.layer.opacity = 0; UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget: self action:@selector (disMiss)] = self.userInteractionEnabled; YES; [self addGestureRecognizer:tap]; [self addSubview:self.contentView]; [self.contentView addSubview:self.picker View]; [self.contentView addSubview:self.cancelButton]; [self.contentView addSubview:self.confirmButton]; [self initPickView];} return self;} / / initialize selector, create sub class to achieve the superclass method - (void) initPickView{} / / click the OK button - (void) clickConfirmButton {[self disMiss];} / / click the Cancel button - (void) clickCancelButton {[self disMiss];} / / display selector (void) show - sharedApplication].keyWindow [self setCenter:[UIApplication {[[UIApplication addSubview:self]; sharedApplication].keyWindow.center] [[UIApplication; sharedApplication].keyWindow bringSubviewToFront:self] CGRect; frame = self.contentView.frame; frame.origin.y = self.co NtentView.frame.size.height; [UIView animateWithDuration:0.4 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{[self.layer setOpacity:1.0]; self.contentView.frame = frame;} completion:^ (BOOL finished) {}]}; / / remove selector - (void) disMiss frame {CGRect = self.contentView.frame; frame.origin.y = self.contentView.frame.size.height; [UIView animateWithDuration:0.4 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{[self.layer setOpacity:0.0]; self.contentView.frame = frame;} completion:^ (BOOL finished) {[self removeFromSuperview];}]}; / / set the height selector - (void) setP IckerViewHeight: (CGFloat pickerViewHeight) {_pickerViewHeight = pickerViewHeight; self.contentView.frame = CGRectMake (self.contentView.frame.origin.x, self.contentView.frame.origin.y, self.contentView.frame.size.width, pickerViewHeight);} - (UIView * contentView) {if (! _contentView) {_contentView alloc]initWithFrame:CGRectMake = [[UIView (0, ScreenHeight, ScreenWidth, self.pickerViewHeight)]; [_contentView setBackgroundColor:[UIColor whiteColor]];}} - (return _contentView; UIPickerView * pickerView) {if (_pickerView!) {_pickerView alloc]initWithFrame:CGRectMake = [[UIPickerView (0, 0, self.contentView.frame.size.width, self.contentView.frame.size.height)]; [_pickerView setBac KgroundColor:[UIColor whiteColor]] return _pickerView;};} - (UIButton * cancelButton) {if (! _cancelButton) {_cancelButton alloc]initWithFrame:CGRectMake = [[UIButton (16, 0, 44, 44)]; [_cancelButton setTitle:@ forState:UIControlStateNormal]; [_cancelButton setTitleColor:[UIColor cancel grayColor] forState:UIControlStateNormal]; [_cancelButton.titleLabel setFont:[UIFont systemFontOfSize:16]]; [_cancelButton addTarget:self action:@selector (clickCancelButton) forControlEvents:UIControlEventTouchUpInside] return _cancelButton;}; (UIButton * confirmButton)} - {if (! _confirmButton) {_confirmButton (self.contentView.frame = [[UIButton alloc]initWithFrame:CGRectMake .size.width - self.cancelButton.frame.size.width - self.cancelButton.frame.origin.x, self.cancelButton.frame.origin.y, self.cancelButton.frame.size.width, self.cancelButton.frame.size.height)]; [_confirmButton setTitle:@ forState: UIControlStateNormal] [_confirmButton "OK"; setTitleColor:[UIColor blueColor] forState:UIControlStateNormal]; [_confirmButton.titleLabel setFont:[UIFont systemFontOfSize:16]]; [_confirmButton addTarget:self action:@selector (clickConfirmButton) forControlEvents:UIControlEventTouchUpInside] return _confirmButton @end;}};
(2) a custom date selector can be selected to display the year, month, year, month, day, and whether to display the “so far” option.

Create a date selector WXZPickDateView
.H inherited from our custom good WXZBasePickView declaration related properties and methods

#import "WXZBasePickView.h" @class WXZBasePickView @protocol PickerDateViewDelegate< / / select agent method selector; NSObject> - (void) pickerDateView: (WXZBasePickView *) pickerDateView selectYear: (NSInteger) year selectMonth: (NSInteger) month selectDay: (NSInteger) day; @end @interface WXZPickDateView: WXZBasePickView @property (nonatomic, weak) ID < PickerDateViewDelegate> delegate; @property (nonatomic assign, BOOL isAddYetSelect); / / now whether to increase the option @property (nonatomic, assign) BOOL isShowDay; / / day / / set whether to display the default display value - (void) setDefaultTSelectYear: (NSInteger) defaultSelectYear defaultSelectMonth: (NSInteger) defaultSelectMonth defaultSelectDay: (NSInteger) defaultSelectDay; @end

.m implementation related methods

#import "WXZPickDateView.h" @interface (WXZPickDateView) < UIPickerViewDataSource, UIPickerViewDelegate> / * * * / @property selected years (nonatomic, assign) NSInteger selectYear; / * * * / month selection @property (nonatomic, assign) NSInteger selectMonth; / * * choose day * / @property (nonatomic, assign) NSInteger selectDay; @property (nonatomic, assign) NSInteger currentYear @property (nonatomic, assign); NSInteger currentMonth; @property (nonatomic, assign) NSInteger currentDay @property (nonatomic, assign); NSInteger defaultYear; @property (nonatomic, assign) NSInteger defaultMonth @property (nonatomic, assign); NSInteger defaultDay; @property (nonatomic, assign) NSInteger minShowYear @property (nonatomic, assign); NSInteger yearSum; @end @implemen Tation WXZPickDateView - (void) initPickView {[super initPickView]; _minShowYear = 1940; / / *gregorian = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian] NSCalendar minimum year; / / get the current date NSDate* DT = [NSDate date]; / / the specified access to the specified date and time, minutes and seconds of information unsigned unitFlags = NSCalendarUnitYear NSCalendarUnitMonth NSCalendarUnitDay NSCalendarUnitHour | | | | NSCalendarUnitMinute | NSCalendarUnitSecond | NSCalendarUnitWeekday; / / get different time field information NSDateComponents* comp components: unitFlags fromDate:dt] = [gregorian; _yearSum = comp.year-_minShowYear+1; _currentYear=comp.year; _currentMonth=comp.mon Th; _currentDay=comp.day; _selectYear = comp.year; _selectMonth = comp.month; _selectDay = comp.day; _defaultYear = comp.year; _defaultMonth = comp.month; _defaultDay=comp.day; [self.pickerView setDelegate:self]; [self.pickerView setDataSource:self];} - (void) setDefaultTSelectYear: (NSInteger) defaultSelectYear defaultSelectMonth: (NSInteger) defaultSelectMonth defaultSelectDay: (NSInteger) defaultSelectDay{if (defaultSelectYear! =0) {_defaultYear= defaultSelectYear}; if (defaultSelectMonth! =0) {_defaultMonth = defaultSelectMonth;} if (defaultSelectDay! =0) {_defaultDay=defaultSelectDay; if (defaultSelectYear==-1) {_defaultYear=_curren} TYear+1; _defaultMonth=1; _defaultDay=1; [self.pickerView} selectRow: (_defaultYear - _minShowYear) inComponent:0 animated:NO]; [self.pickerView reloadComponent:1]; [self.pickerView selectRow: (_defaultMonth - 1) inComponent:1 animated:NO]; if (_isShowDay==YES) {[self.pickerView reloadComponent:2]; [self.pickerView selectRow:_defaultDay inComponent:1 animated:NO] [self refreshPickViewData];};} - (void) setIsAddYetSelect: (BOOL) isAddYetSelect{- _isAddYetSelect = isAddYetSelect;} (NSInteger) numberOfComponentsInPickerView: (UIPickerView * pickerView) {/ / to determine whether or not to display on the display, if it returns 3, otherwise return 2 if (_isShowDay==YES) {return 3;} Else{return 2;}} - (NSInteger) pickerView: (UIPickerView *) pickerView numberOfRowsInComponent: (NSInteger component) {if (component = = 0) {{/ / if (_isAddYetSelect==YES) has display options, need to return a line of return self.yearSum+1 more than a total of the year to show}else{return; self.yearSum}else;} if (component = = 1) {NSInteger yearSelected = [pickerView selectedRowInComponent:0] + self.minShowYear; if (yearSelected==_currentYear+1) {/ / has options when month information does not return return 0;}else{return 12;}}else {NSInteger yearSelected = [pickerView selectedRowInComponen T:0] + self.minShowYear; if (yearSelected==_currentYear+1) {/ / has options when information does not return return 0;}else{NSInteger yearSelected = [pickerView selectedRowInComponent:0] + self.minShowYear; NSInteger monthSelected = [pickerView selectedRowInComponent:1] + 1 return [self; getDaysWithYear:yearSelected month:monthSelected];}}} / / according to the year month date of judgment days - (NSInteger) getDaysWithYear: (NSInteger) year month: (NSInteger) month (month) {switch {case 1: return 31; break; case 2: if (year%400==0 || (year%100! =0 & & year%4 = = 0)) {return 29; }else{return 28 break case 3:;}; return 31; break case; 4: return 30; break case; 5: return 31; break case; 6: return 30; break case; 7: return 31; break case; 8: return 31; break case; 9: return 30; break case; 10: return 31; break; case 11: return 30; break case; 12: break; default: return 31; return 0; break;}} - (CGFloat) pickerView: (UIP IckerView * pickerView rowHeightForComponent: component (NSInteger)) {/ / the height of each row of return 36;} - (void) pickerView: (UIPickerView *) pickerView didSelectRow: (NSInteger) row inComponent: (NSInteger) component selectYear NSInteger {NSInteger; selectMonth; switch (component) {case 0: [pickerView reloadComponent:1]; if (_isAddYetSelect==YES) {selectYear = row+_minShowYear+1}else{; selectYear = row+_minShowYear;} if (_isShowDay==YES) {[pickerView reloadComponent:2]}; break case 1:; selectMonth = row+1; if (_isShowDay==YES) {[pi CkerView reloadComponent:2] default: break;}}; [self refreshPickViewData];} - (UIView *) pickerView: (* UIPickerView) pickerView viewForRow: (NSInteger) row forComponent: (NSInteger) component reusingView: (nullable UIView * view) {NSString *text; if (component = = 0) {if (_isAddYetSelect==YES) {if (row+_minShowYear==_currentYear+1) {text=@ "so far"}else{[NSString stringWithFormat:@ "; text =%zd, row + _minShowYear]}else{text [NSString stringWithFormat:@;} ="%zd ", row + _minShowYear];}else} if (component = = 1) {if {NSI (_isAddYetSelect==YES) Nteger yearSelected = [pickerView selectedRowInComponent:0] + self.minShowYear; if (yearSelected==_currentYear+1) {text = [NSString "stringWithFormat:@"];}else{text = [NSString stringWithFormat:@%zd ", row + 1];}}else{text = [NSString stringWithFormat:@"%zd, row "1] + text =}else{;} [NSString stringWithFormat:@"%zd "row +. 1] UILabel *label alloc]init];} = [[UILabel; label.textAlignment = 1; label.font = [UIFont systemFontOfSize:16]; label.text = text; return label;} - (void) clickConfirmButton (if [self.delegate respondsToSelector:@selector pickerDateView:selectYear:selectMo ({ Nth:selectDay:)] pickerDateView:self selectYear:self.selectYear selectMonth:self.selectMonth selectDay:self.selectDay]) {[self.delegate}; [super clickConfirmButton];} - {self.selectYear = refreshPickViewData (void) [self.pickerView selectedRowInComponent:0] + self.minShowYear = [self.pickerView + 1; self.selectMonth selectedRowInComponent:1]; if (_isShowDay==YES) {self.selectDay [self.pickerView = selectedRowInComponent:2] + 1;}} - (void) setYearLeast: (NSInteger yearLeast) {_minShowYear = yearLeast;} - (void) setYearSum: (NSInteger yearSum) {_yearSum = yearSum;} - (void) setIsShowDay: (BOOL) isShowDay{_isShowDay=isShowDay @end;}
(3) to create a time selector WXZBasePickView inherited from WXZPickTimeView.

.h declaration properties and related methods

#import "WXZBasePickView.h" @class WXZPickTimeView; @protocol PickTimeViewDelegate< NSObject> - (void) pickerTimeView: (WXZPickTimeView *) pickerTimeView selectHour: (NSInteger) hour selectMinute: (NSInteger) minute; @end WXZPickTimeView @property @ interface: WXZBasePickView (nonatomic, weak) ID < PickTimeViewDelegate> delegate; - (void) setDefaultHour: (NSInteger) hour defaultMinute: (NSInteger minute; @end)

.m implementation related methods

#import "WXZPickTimeView.h" @interface (WXZPickTimeView) < UIPickerViewDataSource, UIPickerViewDelegate> UIDatePicker {*_datePicker}; @property (nonatomic, assign) NSInteger selectHour @property (nonatomic, assign); NSInteger selectMinute; @property (nonatomic, assign) NSInteger day @property (nonatomic, assign); NSInteger defaultHour; @property (nonatomic, assign) NSInteger defaultMinute; @end @implementation WXZPickTimeView (void) initPickView initPickView] NSCalendar {[super = [[NSCalendar; *gregorian alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; / / get the current date NSDate* DT = [NSDate date]; / / define a time field flag, will be specified Take a specified date and time, minutes and seconds of information unsigned unitFlags = NSCalendarUnitYear NSCalendarUnitMonth NSCalendarUnitDay NSCalendarUnitHour | | | | NSCalendarUnitMinute | | NSCalendarUnitSecond NSCalendarUnitWeekday; / / get different time field information NSDateComponents* comp = [gregorian components: unitFlags fromDate:dt]; _selectHour=comp.hour; _selectMinute=comp.minute; [self.pickerView setDelegate:self]; [self.pickerView setDataSource:self] [self.pickerView; selectRow:_selectHour inComponent:0 animated:NO]; [self.pickerView selectRow:_selectMinute inComponent:1 animated:NO];} - (void) setDefaultHour: (NSInteger) hour defaultMinute: (NS Integer if (minute{) hour! =0) {_defaultHour=hour};}else{_defaultHour=_selectHour; if (minute! =0) {_defaultMinute = minute;}else{_defaultMinute=_selectMinute; [self.pickerView selectRow:_defaultHour inComponent:0} animated:NO]; [self.pickerView reloadComponent:1] [self.pickerView; selectRow:_defaultMinute inComponent:1 animated:NO]; [self refreshPickTimeViewData];} - (NSInteger) numberOfComponentsInPickerView: (UIPickerView * pickerView) {return} - 2; (NSInteger) pickerView: (UIPickerView *) pickerView numberOfRowsInComponent: (NSInteger component) {if (component = = 0) {}else {return 24; return 60;}} - (CGFloat) pickerView : (UIPickerView *) pickerView rowHeightForComponent: (NSInteger) component {return 36;} - (void) pickerView: (UIPickerView *) pickerView didSelectRow: (NSInteger) row inComponent: (NSInteger) component (component) {switch {case 0: break; [pickerView reloadComponent:1]; case 1: default: break [self refreshPickTimeViewData];};} - (UIView *) pickerView: (UIPickerView *) pickerView viewForRow: (NSInteger) row forComponent: (NSInteger) component reusingView: (nullable UIView * view) {NSString *text; if (component = = 0) {if (row< 10) {text = "0%zd [NSString stringWithFormat:@" row] text = [NSString stringWithForma;}else{ T:@ "%zd" row];}else} if (component = = 1) {if (row< 10) {text = "stringWithFormat:@ [NSString 0%zd", row}else{[NSString stringWithFormat:@]; text = "%zd", row}else{UILabel *label}]}; [[UILabel alloc] = init]; label.textAlignment = 1; label.font [UIFont = systemFontOfSize:16]; label.text = text; return label;} - {(void) clickConfirmButton if ([self.delegate respondsToSelector:@selector (pickerTimeView:selectHour:selectMinute:)] [self.delegate pickerTimeView:self selectHour:self.selectHour) {selectMinute:self.selectMinute]}; [super clickConfirmButton];} - {self.selectHou (void) refreshPickTimeViewData R = [self.pickerView selectedRowInComponent:0]; self.selectMinute = [self.pickerView selectedRowInComponent:1];} @end
(4) create a city selector WXZBasePickView inherited from WXZPickCityView.

.h declaration related attributes and methods

#import "WXZBasePickView.h" @class WXZPickCityView; @protocol PickerCityViewDelegate< NSObject> - (void) pickerArea: (WXZPickCityView * pickerArea) selectProvince: (NSString * province) selectCity: (NSString * city); @end WXZPickCityView @property @ interface: WXZBasePickView (nonatomic, weak) ID < PickerCityViewDelegate> delegate; / * * * / - default provinces (void) setDefaultCity: (NSString * defaultCity) DefaultProvience: (NSString * defaultProvience); @end

.m implementation related methods

#import "WXZPickCityView.h" @interface (WXZPickCityView) < UIPickerViewDataSource, UIPickerViewDelegate> @property (nonatomic, strong, nullable) NSArray *areaDataSource; @property (nonatomic, strong, nullable) NSMutableArray *provinceArray; @property (nonatomic, strong, nullable) NSMutableArray *cityArray; @property (nonatomic, strong, nullable) NSMutableArray *selectedArray; / / the currently selected array @property (nonatomic, strong, nullable) NSString *selectProvince; @property (nonatomic, strong, nullable) NSString *selectCity; @end @implementation WXZPickCityView (void) - initPickView (for {[super initPickView]; NSDictionary *dic in self.areaDataSource) {[self.provinceArray addObject:dic[@ "state" NSMuta ";} BleArray *citysArr = [NSMutableArray arrayWithArray:[self.areaDataSource firstObject][@ "cities"; "for (NSDictionary *dic in citysArr [self.cityArray addObject:dic[@) {" city "}"; self.selectProvince = self.provinceArray[0]; self.selectCity = self.cityArray[0]; [self.pickerView setDelegate:self]; [self.pickerView setDataSource:self];} / / set the default display and - (void) setDefaultCity: (NSString * defaultCity) DefaultProvience: (NSString * defaultProvience{) for (NSInteger I = 0; i< _provinceArray.count; i++) {if ([_provinceArray[i] isEqualToString:defaultProvience]) {[self.pickerView selectRow:i = self.areaDataSou inComponent:0 animated:NO]; self.selectedArray Rce[i][@ "cities"]; [self.cityArray removeAllObjects]; [self.selectedArray enumerateObjectsUsingBlock:^ (NSDictionary *obj, NSUInteger IDX, BOOL * _Nonnull stop) {[self.cityArray "city" addObject:obj[@ "}]; for (NSInteger; j= 0; j< _cityArray.count; j++) {if ([defaultCity isEqualToString:_cityArray[j]]) {[self.pickerView selectRow:i inComponent:0 animated:NO]; [self.pickerView reloadComponent:1]; [self.pickerView selectRow:j inComponent:1 animated:NO] [self; refreshSelectAreaData];}}}}} - (NSInteger) numberOfComponentsInPickerVi Ew: (UIPickerView * pickerView) {return 2;} - (NSInteger) pickerView: (UIPickerView *) pickerView numberOfRowsInComponent: (NSInteger component) {if (component = = 0) {{return self.provinceArray.count;}else return self.cityArray.count;}} - (CGFloat) pickerView: (UIPickerView *) pickerView rowHeightForComponent: (NSInteger) component {return 36;} - (void) pickerView: (UIPickerView *) pickerView didSelectRow: (NSInteger) row inComponent: (NSInteger component) {if (component = = 0) {self.selectedArray self.areaDataSource[row][@ = "cities"]; [self.cityArray removeAllObjects]; [self.selectedArray enumerateObjectsUsingBlock:^ (NSDictionary *obj, NSUInteger IDX, BOOL * _Nonnull stop) { [self.cityArray addObject:obj[@ "city" [pickerView "}]; reloadComponent:1]; [pickerView; selectRow:0 inComponent:1 animated:YES];}else if (component = = 1) {if (self.selectedArray.count = = 0) {self.selectedArray = [self.areaDataSource firstObject][@]" cities ";}else{[self refreshSelectAreaData]}};} - (UIView *) pickerView: (* UIPickerView) pickerView viewForRow: (NSInteger) row forComponent: (NSInteger) component reusingView: (nullable UIView * view) {for (UIView *singleLine in pickerView.subviews if (singleLine.frame.size.height) {< 1; singleLine.backgroundColor) {=[UIColor grayColor]}}; NSString *text ; if (component = = 0) {text = self.provinceArray[row];}else if (component = = 1) {text = self.cityArray[row];}else{= [[UILabel} UILabel *label alloc]init]; label.textAlignment = 1; label.font = [UIFont systemFontOfSize:16]; label.text = text; return label;} - (void) clickConfirmButton pickerArea:self selectProvince:self.selectProvince {[self.delegate selectCity:self.selectCity]; [super clickConfirmButton];} - (void) refreshSelectAreaData NSInteger [self.pickerView selectedRowInComponent:0] {provienceIndex = NSInteger; cityIndex = [self.pickerView = selectedRowInComponent:1]; self.selectProvince = self.provinceArray[provienceIndex]; self.selectCity Self.cityArray[cityIndex];} - (NSArray * areaDataSource) {if (! _areaDataSource) {NSString *path = [[NSBundle bundleForClass:[WXZPickCityView class]] pathForResource:@ "area" ofType:@ "plist"]; _areaDataSource = [[NSArray alloc] initWithContentsOfFile:path] return _areaDataSource;};} - (NSMutableArray * provinceArray) {if (! _provinceArray) {_provinceArray} = [NSMutableArray array]; return _provinceArray - (* NSMutableArray);} cityArray {if (_cityArray! = [NSMutableArray) {_cityArray array]}; return _cityArray;} - (NSMutableArray * selectedArray) {if (_selectedArray! = [NSMutableArray) {_selectedArray array]}; return _selectedArray;} @end
(5) to create a single row selector WXZBasePickView inherited from WXZCustomPickView, can be displayed in accordance with the array to display the relevant selection

.h declaration related attributes and methods

#import "WXZBasePickView.h" @class WXZCustomPickView; @protocol CustomPickViewDelegate< NSObject> - (void) customPickView: (WXZCustomPickView * customPickView) selectedTitle: (NSString * selectedTitle); @end @interface WXZCustomPickView: WXZBasePickView @property (nonatomic, strong) NSMutableArray *dataArray @property (nonatomic, copy); NSString *defalutSelectRowStr; @property (nonatomic, weak) ID < CustomPickViewDelegate> delegate @end;

.m implementation related methods

#import "WXZCustomPickView.h" @interface (WXZCustomPickView) < UIPickerViewDataSource, UIPickerViewDelegate> / * * 1 selected string / @property (nonatomic, strong, nullable) NSString *selectedTitle; @end @implementation WXZCustomPickView (void) initPickView initPickView] _dataArray=[NSMutableArray {[super; mutableCopy]; [self.pickerView setDelegate:self]; [self.pickerView setDataSource:self];} - (void) setDefalutSelectRowStr: (NSString *) defalutSelectRowStr{_defalutSelectRowStr=defalutSelectRowStr for (NSInteger; I = 0; i< _dataArray.count; i++) {if ([_dataArray[i] isEqualToString:_defalutSelectRowStr]) {[self.pickerView reloadAllComponents]; [self.pickerVie W selectRow:i inComponent:0 animated:NO];}}} - (NSInteger) numberOfComponentsInPickerView: (UIPickerView * pickerView) {return 1;} - (NSInteger) pickerView: (UIPickerView *) pickerView numberOfRowsInComponent: (NSInteger) component {return self.dataArray.count;} - (CGFloat) pickerView: (UIPickerView *) pickerView rowHeightForComponent: (NSInteger component) {return} - 36; (CGFloat) pickerView: (UIPickerView *) pickerView widthForComponent: (NSInteger) component {return self.frame.size.width;} - (void) pickerView: (UIPickerView *) pickerView didSelectRow: (NSInteger) row inComponent: (NSInteger component) {self.selectedTitle = self.dataArray[row];} - (UIView *) pickerView: (UIPickerView * pickerView viewForRow: (N) SInteger row forComponent: (NSInteger) component reusingView: (nullable) UIView * view (UIView) {for *singleLine in pickerView.subviews if (singleLine.frame.size.height) {< 1; singleLine.backgroundColor) {=[UIColor}} = *label grayColor]; UILabel [[UILabel alloc]init]; label.font=[UIFont systemFontOfSize:16]; label.textAlignment = 1; label.text=self.dataArray[row]; return label;} - #pragma mark - click the OK button (void) clickConfirmButton customPickView:self selectedTitle:self.selectedTitle] [super {[self.delegate; clickConfirmButton];} - (void) setDataArray: (NSMutableArray * dataArray) {_dataArray = dataArray; _selectedTitle = dataArray.fir StObject; [self.pickerView reloadAllComponents];} @end

In this way, we have defined several types of selectors, where the need for selectors, according to the needs of the corresponding selector can be created.

Four, how to use a custom selector has been created

The introduction of a good custom selector, declare the relevant attributes, signed the relevant agreement

#import "ViewController.h" #import "WXZPickDateView.h" #import "WXZPickAreaView.h" #import "WXZPickTimeView.h" #import "WXZCustomPickView.h" @interface (ViewController) < PickerDateViewDelegate, PickerAreaViewDelegate, PickTimeViewDelegate, CustomPickViewDelegate> {UIButton *dateButton; UIButton; *selectAreabutton; UIButton *selectTimeButton; UIButton *singlePickViewSelectButton; BOOL isShowDay;} / / whether to display the information of @end

Create the corresponding button to trigger the corresponding selector

- (void) viewDidLoad viewDidLoad] dateButton=[UIButton {[super; buttonWithType:UIButtonTypeCustom]; dateButton.frame=CGRectMake (100, 100, 100, 50); [dateButton setTitle:@ forState:UIControlStateNormal]; [self.view "date" addSubview:dateButton]; [dateButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [dateButton addTarget:self action:@selector (pickViewSelect:) forControlEvents:UIControlEventTouchUpInside]; dateButton.tag = 1000; selectAreabutton=[UIButton buttonWithType:UIButtonTypeCustom]; selectAreabutton.frame=CGRectMake (100, 200, 100, 50) [selectAreabutton; setTitle:@ forState:UIControlStateNormal]; [self.view "region" addSubview:selectAreabutton]; [selectAreabutton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [selectAreabutton addTarget:self action:@selector (pickViewSelect:) forControlEvents:UIControlEventTouchUpInside]; selectAreabutton.tag = 1001; selectTimeButton= [UIButton buttonWithType:UIButtonTypeCustom]; selectTimeButton.frame=CGRectMake (100, 300, 100, 50); [selectTimeButton setTitle:@ forState:UIControlStateNormal]; [self.view "time" addSubview:selectTimeButton]; [selectTimeButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [selectTimeButton addTarget:self action:@selector (pickViewSelect:) forControlEvents:UIControlEventTouchUpInside]; selectTimeButton.tag = 1002; singlePickViewSel EctButton=[UIButton buttonWithType:UIButtonTypeCustom]; singlePickViewSelectButton.frame=CGRectMake (100, 400, 100, 50); [singlePickViewSelectButton setTitle:@ "single data selector" forState:UIControlStateNormal]; [self.view addSubview:singlePickViewSelectButton]; [singlePickViewSelectButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [singlePickViewSelectButton addTarget:self action:@selector (pickViewSelect:) forControlEvents:UIControlEventTouchUpInside]; singlePickViewSelectButton.tag = 1003;}

Button click event:

- (void) pickViewSelect: (UIButton *) button{if (button.tag==1000) {WXZPickDateView *pickerDate = [[WXZPickDateView alloc]init]; [pickerDate setIsAddYetSelect:YES]; / / [pickerDate setIsShowDay:YES] option is displayed so far; / / [pickerDate setDefaultTSelectYear:2007 defaultSelectMonth:4 on whether to display the information of defaultSelectDay:1]; / / set the default display date of [pickerDate setValidTime:2010]; [pickerDate setDelegate:self]; [pickerDate show];}else if (button.tag==1001) {WXZPickAreaView * pickerArea = [[WXZPickAreaView alloc]init]; [pickerArea setDelegate:self]; [pickerArea setDefaultCity:@ "Shanghai" DefaultProvience:@ "Shanghai"]; [pickerArea show]; [self.view endEditing:YES];}else if (button.tag==1002) {WXZPickTimeView *pickerArea = [[WXZPickTimeView alloc]init]; [pickerArea setDelegate:self]; [pickerArea setDefaultHour:14 defaultMinute:20]; [pickerArea show]; endEditing:YES]}else{NSMutableArray *arrayData self.view; arrayWithObjects:@ [NSMutableArray = "2K", "2k-5k", "@ @ 5k-10k, @ 10k-15k, @ 15k-25k, @" 25k-50k "," 50K ", @ nil]; WXZCustomPickView * pickerSingle = [[WXZCustomPickView alloc]init]; [pickerSingle setDataArray:arrayData]; [pickerSingle setDefalutSelectRowStr:arrayData[0]]; [pickerSingle setDelegate:self]; [pickerSingle show]; [self.view endEditing:YES];}}

Proxy method of selector

- (void) pickerDateView: (WXZBasePickView *) pickerDateView selectYear: (NSInteger) year selectMonth: (NSInteger) month selectDay: (NSInteger) day{NSLog (@ "the date is:%ld%ld%ld, year, month, day); if (isShowDay==YES) {[dateButton setTitle:[NSString stringWithFormat:@"%ld year%ld month%ld day, year month, day], forState:UIControlStateNormal];}else{[dateButton setTitle:[NSString stringWithFormat:@ "%ld%ld", year month], forState: UIControlStateNormal];}} - (void) pickerArea: (WXZPickAreaView * pickerArea) selectProvince: (NSString * province) selectCity: (NSString *) city{NSLog (@ "% @ provinces selection% @" province, [selectAreabutton, city); setTitle:[NSString stringWithFormat:@ Province, city] forState:UIC,%@%@ " OntrolStateNormal];} - (void) customPickView: (WXZCustomPickView * customPickView) selectedTitle: (NSString *) selectedTitle{NSLog (@ "% @", selectedTitle); [singlePickViewSelectButton setTitle:selectedTitle forState: UIControlStateNormal];} - (void) pickerTimeView: (WXZPickTimeView *) pickerTimeView selectHour: (NSInteger) hour selectMinute: (NSInteger) minute{NSLog (@ "choice of time:%ld%ld, hour, minute); [selectTimeButton setTitle:[NSString stringWithFormat:@"%ld%ld ", hour minute] forState:UIControlStateNormal];}

Finally, look at the renderings:

IOS custom date, time, city selector
selector demo renderings

There is a need to download demo look

Demo address