UIDatePicker应用 常用属性和方法

 1 #import "ViewController.h"
 2 
 3 @interface ViewController ()
 4 @property(strong,nonatomic)UIDatePicker * datepicker;
 5 -(void)UpdataTime;
 6 @end
 7 
 8 @implementation ViewController
 9 
10 - (void)viewDidLoad {
11     [super viewDidLoad];
12     // Do any additional setup after loading the view, typically from a nib.
13     self.datepicker = [[UIDatePicker alloc]initWithFrame:CGRectMake(10, 100, 350, 100)];
14     self.datepicker.backgroundColor = [UIColor whiteColor];
15     self.datepicker.datePickerMode = UIDatePickerModeDateAndTime;
16     //设置地区,即显示的语言
17     self.datepicker.locale = [[NSLocale alloc]initWithLocaleIdentifier:@"zh_Hans_CN"];
18     //设置当前日历
19     [self.datepicker setCalendar:[NSCalendar currentCalendar]];
20     //使得时间和系统同步
21     [self.datepicker setTimeZone:[NSTimeZone systemTimeZone]];
22     
23     [self.datepicker setDate:[NSDate date] animated:YES];
24     
25     [self.datepicker addTarget:self action:@selector(UpdataTime) forControlEvents:UIControlEventValueChanged];
26     [self.view addSubview:self.datepicker];
27     
28     
29 }
30 -(void)UpdataTime
31 {
32     [self.datepicker setDate:[NSDate date] animated:YES];
33 }
34 - (void)didReceiveMemoryWarning {
35     [super didReceiveMemoryWarning];
36     // Dispose of any resources that can be recreated.
37 }
38 
39 @end
View Code

1、常用属性

//设置pickerview的显示模式

self.datepicker.datePickerMode = UIDatePickerModeDateAndTime;

其中UIDatePickerModeCountDownTimer 是倒计时模式,需用到

countDownDuration这个属性

//设置地区,即显示的语言

self.datepicker.locale = [[NSLocale alloc]initWithLocaleIdentifier:@"zh_Hans_CN"];

//设置当前日历

[self.datepicker setCalendar:[NSCalendar currentCalendar]];

//使得时间和系统同步

[self.datepicker setTimeZone:[NSTimeZone systemTimeZone]];

//

date属性可读可写,可以从pickerview上读取数据显示到其他上面

//目标动作回调,实时监听数据更新

[self.datepicker addTarget:self action:@selector(UpdataTime) forControlEvents:UIControlEventValueChanged];

2、方法

//设置时间

self.datepicker setDate:[NSDate date] animated:YES];

原文地址:https://www.cnblogs.com/ylg-----/p/4772808.html