UIdatePicker的使用
1. 初始化UIDatePicker
UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 200, 320, 216)];
注:高度和宽度可以设定为0,比如:
UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 200, 0, 0)];
2. 设置时区
[datePicker setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
3. 设置当前显示时间
[datePicker setDate:tempDate animated:YES];
4. 设置显示最大时间(此处为当前时间)
[datePicker setMaximumDate:[NSDate date]];
或
datePicker.maximumDate = [NSDate date];
设置最小时间
NSDate* minDate = [[NSDate alloc]initWithString:@"1900-01-01 00:00:00 -0500"];
datePicker.minimumDate = minDate;
[minDate release];
5. 设置UIDatePicker的显示模式
[datePicker setDatePickerMode:UIDatePickerModeDate];
参数 UIDatePickerMode 有四种:
UIDatePickerModeTime, // Displays hour, minute, and optionally AM/PM designation depending on the locale setting (e.g. 6 | 53 | PM)
UIDatePickerModeDate, // Displays month, day, and year depending on the locale setting (e.g. November | 15 | 2007)
UIDatePickerModeDateAndTime, // Displays date, hour, minute, and optionally AM/PM designation depending on the locale setting (e.g. Wed Nov 15 | 6 | 53 | PM)
UIDatePickerModeCountDownTimer // Displays hour and minute (e.g. 1 | 53)
6. 当值发生改变的时候调用的方法
[datePicker addTarget:self action:@selector(datePickerValueChanged:) forControlEvents:UIControlEventValueChanged];
7. 加入到视图
[self.view addSubview:datePicker];
甚至还可以加入到 UIActionSheet 中
[actionSheet addSubview: datePicker];
8. 释放对象
[datePicker release];
9. 获得当前UIPickerDate所在的时间
NSDate *date = [datePicker date];
10.设置本地化语言
NSLocale * locale = [[NSLocalealloc] initWithLocaleIdentifier:@"Chinese"];//设置本地化语言,本地化语言可以通过在xib文件中添加一个日历选取器,然后查看其属性来找到需要的值
[datePicker setLocale:locale];
比如可以在 datePickerValueChanged 回调方法中
- (void)datePickerValueChanged:(id)sender {
UIDatePicker *datePicker = sender;
NSDate *date = [datePicker date];
// TODO:
|