码迷,mamicode.com
首页 > Windows程序 > 详细

UIDataPicker 时间选择器

时间:2015-11-13 18:46:44      阅读:315      评论:0      收藏:0      [点我收藏+]

标签:

自用时间选择器

 

 1 @interface ViewController ()
 2 {
 3     UILabel *cityLabel;
 4     UIDatePicker *datePicker;
 5 }
 6 //@property(nonatomic,strong)ZHPickView *pickview;
 7 @end
 8 
 9 @implementation ViewController
10 
11 - (void)viewDidLoad {
12     [super viewDidLoad];
13     // Do any additional setup after loading the view, typically from a nib.
14     cityLabel=[[UILabel alloc]init];
15     cityLabel.frame=CGRectMake(50, 100, 300, 50 );
16     cityLabel.text=@"abcdefg";
17     [self.view addSubview:cityLabel];
18     
19     UIButton *btn=[[UIButton alloc]init];
20     btn.frame=CGRectMake(100, 300, 100, 100);
21     btn.backgroundColor=[UIColor yellowColor];
22     [btn addTarget:self action:@selector(add) forControlEvents:UIControlEventTouchUpInside];
23     [self.view addSubview:btn];
24     
25     //datePicker宽度320像素和高度216像素,系统都设置好了,只需设置一下他的远点坐标,
26     datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 400,[UIScreen mainScreen].bounds.size.width, 216)];
27     NSLocale *locale = [[NSLocale alloc]initWithLocaleIdentifier:@"zh_CN"];
28     datePicker.locale = locale;
29     //设置datePicker显示模式
30     [datePicker setDatePickerMode:UIDatePickerModeDate];
31     //DatePicker属于UIControl子类,可以触发事件,当滚动滑轮滑轮停下后就调用这个方法了
32     [datePicker addTarget:self action:@selector(dateChanged:) forControlEvents:UIControlEventValueChanged];
33     datePicker.hidden=YES;
34     [self.view addSubview:datePicker];
35 }
36 
37 
38 
39 -(void)add{
40     datePicker.hidden=NO;
41 }
42 
43 -(void)dateChanged:(id)sender
44 {
45     UIDatePicker *control = (UIDatePicker*)sender;
46     //把当前控件设置的时间赋给date
47     NSDate *date = control.date;
48     // 将NSDate格式装换成NSString类型
49     NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
50     //设置日历显示格式
51     [dateFormatter setDateFormat:@"yyyy/MM/dd"];
52     //把日历时间传给字符串
53     NSString *strDate = [dateFormatter stringFromDate:date];
54     NSString *message = [[NSString alloc]initWithFormat:@"%@",strDate];
55     cityLabel.text=message;
56     
57 }

 

UIDataPicker 时间选择器

标签:

原文地址:http://www.cnblogs.com/-yun/p/4962815.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!