UIDatePicker的使用

UIDatePicker是时间选择视图控件,类似于老虎机。

1、通常初始化时,只设置其原点坐标,而不设置其大小(默认大小为frame = (0 176; 320 216)

2、主要用来进行时间的设置


[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. @interface ViewController ()  
  2. //{  
  3. //    // 方法1 设置成变量  
  4. //    UIDatePicker *datePicker;  
  5. //}  
  6.   
  7. // 方法2 设置成属性  
  8. @property (nonatomicstrongUIDatePicker *datePicker;  
  9.   
  10. @end  

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. self.datePicker = [[UIDatePicker alloc] init];  
  2. self.datePicker.center = self.view.center;  
  3. NSLog(@"datePicker %@"self.datePicker);  
  4. [self.view addSubview:self.datePicker];  
  5. self.datePicker.backgroundColor = [UIColor orangeColor];  


[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. // 时间类型  
  2. self.datePicker.datePickerMode = UIDatePickerModeDateAndTime;  

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. // 显示时间,默认当天  
  2. NSDate *defaultDate = [NSDate dateWithTimeIntervalSinceNow:(22 * 224 * 660 * 60)];  
  3. // 方法1  
  4. //    self.datePicker.date = defaultDate;  
  5. // 方法2  
  6. [self.datePicker setDate:defaultDate animated:YES];  

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. // 最小时间  
  2. NSDate *minimumDate = [NSDate dateWithTimeIntervalSinceNow:(-11 * 224 * 660 * 60)];  
  3. self.datePicker.minimumDate = minimumDate;  

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. // 最大时间  
  2. NSDate *maximumDate = [NSDate dateWithTimeIntervalSinceNow:(66 * 224 * 660 * 60)];  
  3. self.datePicker.maximumDate = maximumDate;  

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. // 结合类型UIDatePickerModeCountDownTimer使用,倒时间距  
  2. //    self.datePicker.countDownDuration = 30.0;  

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. // 时间选择秒数的间隔  
  2. self.datePicker.minuteInterval = 5.0;  

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. // 获取当前时间  
  2. UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0.0, (CGRectGetHeight(self.view.bounds) - 40.0), CGRectGetWidth(self.view.bounds), 40.0)];  
  3. [self.view addSubview:button];  
  4. button.backgroundColor = [UIColor redColor];  
  5. [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];  
  6. [button setTitleColor:[UIColor yellowColor] forState:UIControlStateHighlighted];  
  7. [button setTitle:@"获取时间" forState:UIControlStateNormal];  
  8. button.titleLabel.numberOfLines = 2;  
  9. [button addTarget:self action:@selector(showTime:) forControlEvents:UIControlEventTouchUpInside];  
  10.   
  11. // 显示时间  
  12. - (void)showTime:(UIButton *)button  
  13. {  
  14.     // 要获得UIDatePicker的时间,需要将UIDatePicker设置成变量,或属性  
  15.     NSDate *selectedDate = self.datePicker.date;  
  16.     NSLog(@"selectedDate %@", selectedDate);  
  17.       
  18.     NSString *title = [NSString stringWithFormat:@"获取时间\n%@", selectedDate];  
  19.     [button setTitle:title forState:UIControlStateNormal];  
  20. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值