UIDatePicker
UIDatePicker:是一個(gè)控制器類壤圃,封裝了 UIPickerView是辕,但是他是UIControl的子類汁展,專門用于接受日期私蕾、時(shí)間和持續(xù)時(shí)長的輸入僵缺。日期選取器的各列會(huì)按照指定的風(fēng)格進(jìn)行自動(dòng)配置,這樣就讓開發(fā)者不必關(guān)心如何配置表盤這樣的底層操作踩叭。你也可以對其進(jìn)行定制磕潮,令其使用任何范圍的日期。
UIDatePicker初始化:
UIDatePicker *datePicker = [[UIDatePicker alloc]initWithFrame:CGRectMake(30, 300, 0, 0)];
[datePicker addTarget:self action:@selector(dateChange:) forControlEvents:UIControlEventValueChanged];//添加觸發(fā)事件
[self.view addSubview:datePicker];
UIDatePicker常用設(shè)置
setLocale:設(shè)置地區(qū)
[datePicker setLocale:[[NSLocale alloc]initWithLocaleIdentifier:@"zh_CN"]];
setCalendar:設(shè)置datePicker的日歷(默認(rèn)為當(dāng)天)
[datePicker setCalendar:[NSCalendar currentCalendar]];
setDatePickerMode:設(shè)置時(shí)間模式,默認(rèn)UIDatePickerModeDateAndTime
[datePicker setDatePickerMode:UIDatePickerModeDate];
setCountDownDuration:設(shè)置倒計(jì)時(shí)的秒數(shù)
[datePicker setCountDownDuration:2*60];
setTimeZone:設(shè)置時(shí)區(qū)
[datePicker setTimeZone:[NSTimeZone localTimeZone]];
設(shè)置最大,最小日期
//設(shè)置一個(gè)時(shí)間戳
NSTimeInterval timeInterval = 60;
NSDate *minDate = [[NSDate alloc]initWithTimeIntervalSinceNow:-timeInterval];
NSDate *maxDate = [[NSDate alloc]initWithTimeIntervalSinceNow:timeInterval];
//設(shè)置最大斤富,最小日期
datePicker.minimumDate = minDate;
datePicker.maximumDate = maxDate;
設(shè)置分鐘間隔
datePicker.minuteInterval = 5;
取出日期
NSDate *select = datePicker.date;
UIPickerView
//初始化一個(gè)pickerView
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 436, 414, 300)];
//設(shè)置背景色
pickerView.backgroundColor = [UIColor whiteColor];
//設(shè)置代理
pickerView.delegate = self;
pickerView.dataSource = self;
[self.view addSubView:pickerView];```
##UIPickerViewDataSource代理方法
-numberOfComponentsInPickerView:設(shè)置列的返回?cái)?shù)量(必須實(shí)現(xiàn))
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 3;
}```
-pickerView:numberOfRowsInComponent:設(shè)置列里邊組件的個(gè)數(shù) component:組件(必須實(shí)現(xiàn))
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
//如果是第一列
if (component == 0)
{
return 10;
}
//如果是第二列
else if (component == 1)
{
return 5;
}
else
{
return 3;
}
}```
##UIPickerViewDelegate代理方法
返回組件的標(biāo)題:有三個(gè)方法都可以用膏潮,不同方法的返回值不同
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
if (component == 0) {
return @"第一列";
} else if (component == 1) {
return @"第二列";
} else {
return @"第三列";
}
}```
pickerView:didSelectRow:inComponent:選擇器選擇的方法 row:被選中的行
//選擇第一列執(zhí)行的方法
if (component == 0) {
NSLog(@"1");
}
//選擇第二列執(zhí)行的方法
else if (component == 1) {
NSLog(@"2");
}
else {
NSLog(@"3");
}
}```
pickerView:widthForComponent:設(shè)置組件的寬度
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
if (component == 0) {
return 100;
} else if (component == 1) {
return 200;
} else {
return 100;
}
}```
pickerView:rowHeightForComponent:設(shè)置列里邊組件的高度
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
if (component == 0) {
return 20;
} else if (component == 1) {
return 25;
} else {
return 30;
}
}```