How iOS customizes date and data source selection controls
This article mainly introduces iOS how to customize the date and data source selection control, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.
Demand
In App development, there are often such functions as date selection (such as birthday, sleep timing, etc.) or provincial and municipal selection. Usually, UI does not use the controls in UI alone, but adds a cancel and confirm button on the basis of UIPickerView.
Characteristics
1. Support common selective data formats. The control integrates multiple formats such as yyyy-MM-dd, yyyy-MM, hh mm, provincial and municipal cascades, custom data sources (2 columns), custom data sources (3 columns), etc.
2. Support both UITextField and event trigger mechanism
3. Support both XIB and pure code
Integration
First drag the XXPickerView folder into the project
Pure code (choose any mode)
# import "ViewController.h" # import "XXTextField.h" # define random (r, g, b, a) [UIColor colorWithRed: (r) / 255.0 green: (G) / 255.0 blue: (B) / 255.0 alpha: (a) / 255.0] # define RandomColor random (arc4random_uniform, arc4random_uniform) @ implementation ViewController- (void) viewDidLoad {[super viewDidLoad]; CGFloat x = 170; CGFloat width = 178; CGFloat height = 30 CGFloat margin = 50; / / Mode-XXTextField * textField = [[XXTextField alloc] init]; textField.frame = CGRectMake (x, 28, width, height); textField.mode = XXPickerViewModeDate; textField.backgroundColor = RandomColor; [self.view addSubview:textField]; / / Mode II XXTextField * textField2 = [[XXTextField alloc] init]; textField2.frame = CGRectMake (x, textField.frame.origin.y + margin, width, height); textField2.mode = XXPickerViewModeYearAndMonth; textField2.backgroundColor = RandomColor; [self.view addSubview:textField2] / Mode 3 XXTextField * textField3 = [[XXTextField alloc] init]; textField3.frame = CGRectMake (x, textField2.frame.origin.y + margin, width, height); textField3.mode = XXPickerViewModeHourAndMinute; textField3.backgroundColor = RandomColor; [self.view addSubview:textField3]; / / Mode IV XXTextField * textField4 = [[XXTextField alloc] init]; textField4.frame = CGRectMake (x, textField3.frame.origin.y + margin, width, height); textField4.mode = XXPickerViewModeProvinceCity; textField4.backgroundColor = RandomColor; [self.view addSubview:textField4] / Mode 5 XXTextField * textField5 = [[XXTextField alloc] init]; textField5.frame = CGRectMake (x, textField4.frame.origin.y + margin, width, height); textField5.mode = XXPickerViewModeProvinceCityAreas; textField5.backgroundColor = RandomColor; [self.view addSubview:textField5]; / / Mode 6 XXTextField * textField6 = [[XXTextField alloc] init]; textField6.frame = CGRectMake (x, textField5.frame.origin.y + margin, width, height); textField6.mode = XXPickerViewModeDataSourceFor2Column; textField6.dataSource = [dataSourceFor2Column mutableCopy]; textField6.backgroundColor = RandomColor [self.view addSubview:textField6]; / / Mode 7 XXTextField * textField7 = [[XXTextField alloc] init]; textField7.frame = CGRectMake (x, textField6.frame.origin.y + margin, width, height);; textField7.mode = XXPickerViewModeDataSourceFor3Column; textField7.dataSource = [dataSourceFor3Column mutableCopy]; textField7.backgroundColor = RandomColor; [self.view addSubview:textField7];} @ end
XIB mode
1. Bind a custom class
2. Drag the line and set the mode
@ interface ViewController () @ property (weak, nonatomic) IBOutlet XXTextField * textField;@end@implementation ViewController- (void) viewDidLoad {[super viewDidLoad]; _ textField.mode = XXPickerViewModeDate;} @ end
Event mode
# import "ViewController.h" # import "XXInputView.h" @ interface ViewController () @ property (weak, nonatomic) XXInputView * inputView;@end@implementation ViewController- (void) viewDidLoad {[super viewDidLoad];}-(IBAction) showClicked: (id) sender {[self.inputView show] }-(XXInputView *) inputView {if (_ inputView = = nil) {XXInputView * inputView = [[XXInputView alloc] initWithFrame:CGRectMake (0, [UIScreen mainScreen] .bounds.size.height, [UIScreen mainScreen] .bounds.size.width, 200) mode:XXPickerViewModeDate dataSource:nil]; inputView.hideSeparator = YES; inputView.completeBlock = ^ (NSString * dateString) {NSLog (@ "selected data:% @", dateString);}; [self.view addSubview:inputView]; self.inputView = inputView;} return _ inputView;} @ end
Thank you for reading this article carefully. I hope the article "how to customize date and data source selection controls in iOS" shared by the editor will be helpful to you. At the same time, I also hope that you will support and follow the industry information channel. More related knowledge is waiting for you to learn!