iOS时间选择器Demo(-)
2016-03-03 15:02
477 查看
@property (nonatomic,strong)UILabel *timeLabel; @property (nonatomic,strong)UIDatePicker *loadDatePickerView;
定义时间选择器<span style="font-family: Arial, Helvetica, sans-serif;">loadDatePickerView,定义label来显示选择的时间</span>
<span style="font-family: Arial, Helvetica, sans-serif;"> </span>
<span style="font-family: Arial, Helvetica, sans-serif;"></span><pre name="code" class="objc">-(void)viewDidLoad{ UIButton *timebtn = [UIButton buttonWithType:UIButtonTypeCustom]; timebtn.frame = CGRectMake(100, 100, KMainWidth - 200, 50); [timebtn setTitle:@"时间选择器" forState:UIControlStateNormal]; [timebtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal]; [timebtn addTarget:self action:@selector(timeBtnAction) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:timebtn]; self.timeLabel = [[UILabel alloc]init]; self.timeLabel.frame = CGRectMake(100, 200, KMainWidth - 200, 50); self.timeLabel.textAlignment = NSTextAlignmentCenter; self.timeLabel.font = [UIFont systemFontOfSize:15]; self.timeLabel.textColor = [UIColor greenColor]; [self.view addSubview:self.timeLabel]; }
#pragma mark - timeBtnAction -(void)timeBtnAction{ if (_loadDatePickerView == nil) { _loadDatePickerView = [[UIDatePicker alloc] init]; //设置时区 _loadDatePickerView.locale = [NSLocale localeWithLocaleIdentifier:@"zh_CN"]; //设置时间模式 _loadDatePickerView.datePickerMode = UIDatePickerModeDateAndTime; //设置最小时间(20年前)3秒 _loadDatePickerView.minimumDate = [NSDate dateWithTimeIntervalSinceNow:-(20 * 365 * 24 * 60 * 60)]; //设置最大时间(20年后) _loadDatePickerView.maximumDate = [NSDate dateWithTimeIntervalSinceNow:(20 * 365 * 24 * 60 * 60)]; //设置时间间隔 设置的值必须能够被60整除 _loadDatePickerView.minuteInterval = 30; //监听时间值改变 [_loadDatePickerView addTarget:self action:@selector(datePickerValueChanged:) forControlEvents:UIControlEventValueChanged]; _loadDatePickerView.frame = CGRectMake(0, self.view.frame.size.height - 170, KMainWidth, 170); [self.view addSubview:_loadDatePickerView]; }else{ self.loadDatePickerView.hidden = NO; } } -(void)datePickerValueChanged:(UIDatePicker *)datePicker{ //获得时间 NSDate *date = (datePicker == nil ? [NSDate date]:datePicker.date); //格式化时间 NSDateFormatter *formatter = [[NSDateFormatter alloc]init]; //时间格式 formatter.dateFormat = @"yyyy-MM-dd EEE HH:mm"; //将时间转换成字符串 self.timeLabel.text = [formatter stringFromDate:date]; self.loadDatePickerView.hidden = YES; }
实现的效果是:点击按钮,弹出时间选择器,选择完时间后,label显示时间,时间选择器自动隐藏。
效果图:
<img src="https://img-blog.csdn.net/20160303162950780?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="" />
<img src="https://img-blog.csdn.net/20160303163002717?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="" />
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- oracle sql日期比较
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- DB2 日期和时间的函数应用说明
- Delphi实现读取系统时间与日期完整实例
- Country使 MS-DOS 子系统能使用国际时间、日期、货币、大小写转换和小数分隔符
- vbs下通过日期查找文件夹的代码
- 用批处理实现的创建带日期的文件夹的代码
- asp格式化日期时间格式的代码
- sql server动态存储过程按日期保存数据示例
- SqlServer中的日期与时间函数
- Oracle 日期的一些简单使用
- C#验证给定字符串形式日期是否合法的方法
- ASP 日期的加减运算实现代码
- PHP计算指定日期所在周的开始和结束日期的方法