本文主要是介绍怎样创建UIDatePicker实例,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1. 初始化UIDatePickerUIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 200, 320, 216)];
注:高度和宽度可以设定为0,比如:
UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 200, 0, 0)];
[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];
[datePicker setDatePickerMode:UIDatePickerModeDate];
参数 UIDatePickerMode 有四种:
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];比如可以在 datePickerValueChanged 回调方法中
- (void)datePickerValueChanged:(id)sender {
UIDatePicker *datePicker = sender;
NSDate *date = [datePicker date];
// TODO:
|
这篇关于怎样创建UIDatePicker实例的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!