好记性不如烂笔头,勤做笔记。
摘要:
1.UIPickVIew 几个重要的属性
(1)datePickerMode
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
UIDatePickerModeTime:显示时分 还有上午下午 12小时制
UIDatePickerModeDate:显示年月日 12小时制
UIDatePickerModeDateAndTime:显示年月日时分 12小时制
UIDatePickerModeCountDownTimer:显示时分,24小时制
根据自己的需求选择相应的datePickerMode属性。
@property (nullable, nonatomic, strong) NSDate *minimumDate; // specify min/max date range. default is nil. When min > max, the values are ignored. Ignored in countdown timer mode @property (nullable, nonatomic, strong) NSDate *maximumDate;
最大最小时间。限制了最大最小时间之后,此控件就可以自己会滚到设定的时间,不会在日期之前停留。
2.UIPickVIew 几个重要的方法
设置一个监听,用来监听pickVie的值的变化
[startPicker addTarget:self action:@selector(startPick:) forControlEvents:UIControlEventValueChanged];
3.使用汇总遇到的问题
选的时间存在8小时的时差解决方案
//获得的时间与本地的时间差8个小时 NSDate *date=sender.date; NSDateFormatter *formatter=[[NSDateFormatter alloc]init]; formatter.dateFormat=@"yy-MM-dd HH:mm:ss"; //经过时间的格式化会变成本地时间 NSString *timeStr=[formatter stringFromDate:date]; NSLog(@"date-----%@",date); NSLog(@"timeStr-------%@",timeStr);
这样就可以解决了。
下边附上我写的demo的主要代码:
// // ViewController.m // demoUIDatePicker // // Created by taoli on 2017/7/20. // Copyright © 2017年 XM. All rights reserved. // #import "ViewController.h" @interface ViewController () { UIDatePicker *startPicker; UIDatePicker *endPicker; } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; startPicker = [[UIDatePicker alloc]initWithFrame:CGRectMake(0, 100, [UIScreen mainScreen].bounds.size.width, 300)]; startPicker.datePickerMode = UIDatePickerModeDateAndTime; NSDate *minDate = [[NSDate alloc]initWithTimeIntervalSinceNow:0]; startPicker.minimumDate = minDate; startPicker.backgroundColor = [UIColor yellowColor]; [startPicker addTarget:self action:@selector(startPick:) forControlEvents:UIControlEventValueChanged]; [self.view addSubview:startPicker]; endPicker = [[UIDatePicker alloc]initWithFrame:CGRectMake(0, 400, [UIScreen mainScreen].bounds.size.width, 300)]; endPicker.datePickerMode = UIDatePickerModeTime; endPicker.minimumDate =startPicker.date; endPicker.backgroundColor = [UIColor yellowColor]; [self.view addSubview:endPicker]; } -(void)startPick:(UIDatePicker*)sender{ //获得的时间与本地的时间差8个小时 NSDate *date=sender.date; NSDateFormatter *formatter=[[NSDateFormatter alloc]init]; formatter.dateFormat=@"yy-MM-dd HH:mm:ss"; //经过时间的格式化会变成本地时间 NSString *timeStr=[formatter stringFromDate:date]; NSLog(@"date-----%@",date); NSLog(@"timeStr-------%@",timeStr); // NSTimeZone *timeZone=[NSTimeZone systemTimeZone]; // NSInteger seconds=[timeZone secondsFromGMTForDate:date]; // NSDate *newDate=[date dateByAddingTimeInterval:seconds]; // NSString *newTimeStr=[formatter stringFromDate:newDate]; // NSLog(@"newDate----%@",newDate); // NSLog(@"newTimeStr-------%@",newTimeStr); // NSLog(@"%@",sender.date); endPicker.minimumDate = sender.date; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
如发现问题,欢迎不吝赐教。
邮箱673658917@qq.com