IOS UIDatePicker控件的简单使用
//**************代码*******************
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSLog(@"timeZoneName %@",[NSTimeZoneknownTimeZoneNames]);
NSLog(@"locale....... %@",[NSLocaleavailableLocaleIdentifiers]);
NSLocale *ch_zh_locale = [[NSLocalealloc] initWithLocaleIdentifier:@"zh_Hans_CN"] ;
//创建datePicker实例变量并初始化
UIDatePicker *datePicker = [[UIDatePickeralloc] initWithFrame:CGRectMake(0,100, 320 , 216)];//无法改变其高和宽,但是可以获取
datePicker.locale = ch_zh_locale; //设置中文显示
[NSDatedate]; //此方法获取的时间是格林尼治时间中国的时间是东八区 + 8小时即可
NSLog(@"current time %@",[NSDatedate]);
NSTimeInterval eigth_Z_CH = 8*60*60;
NSDate *currentDate = [NSDatedateWithTimeIntervalSinceNow:eigth_Z_CH]; //中国的当前时间
NSLog(@"......%@",currentDate);
/*
UIDatePickerModeTime 只显示具体的时间,不显示年月,星期
UIDatePickerModeDate 只显示 年月日
UIDatePickerModeDateAndTime 年月日 时间 星期 都显示
UIDatePickerModeCountDownTimer 显示多少小时 多少分钟
*/
[datePicker setDatePickerMode:UIDatePickerModeDateAndTime];//设置显示模式
[datePicker setTimeZone:[NSTimeZonetimeZoneWithName:@"Asia/Shanghai"]];//设置时区,上海时区
[datePicker setMinimumDate:[NSDatedate]]; //设置控件的最小时间,超出最小时间还小则会弹回,此时时间为中国时间
[datePicker setMaximumDate:[NSDatedateWithTimeIntervalSinceNow:30*24*60*60]];//设置最大时间,超出也会弹回
[datePicker addTarget:selfaction:@selector(timeChanged:)forControlEvents:UIControlEventValueChanged];//关联方法
[self.view addSubview:datePicker];
}
- (void)timeChanged:(id)sender
{
UIDatePicker *currentPicker = (UIDatePicker *) sender;
NSDate *pickerDate = [currentPicker date];//格林尼治时间
NSLog(@"datepicker current time %@",[pickerDatedateByAddingTimeInterval:8*60*60]);// + 8 输出的格式是24制
}
//**********************代码结束*************************
QQ群技术交流:77081333 ,需要代码的可私信