码迷,mamicode.com
首页 > 其他好文 > 详细

UIDatePicker 时间滚动表

时间:2015-12-19 12:22:18      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:

UIDatePicker *datapicker; //时间滚动表

datapicker = [[UIDatePicker alloc]initWithFrame:CGRectMake(50, 200, 250, 100)];

datapicker.datePickerMode = UIDatePickerModeDate;

NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];

datapicker.locale = locale;

NSDate *select = datapicker.date;

datapicker.maximumDate =select;

[datapicker addTarget:self action:@selector(agepicker) forControlEvents:UIControlEventValueChanged];

[informationView addSubview:datapicker];

agelabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 55, Kwidth, 25)];

agelabel.backgroundColor = [UIColor redColor];

agelabel.textAlignment = NSTextAlignmentLeft;

[informationView addSubview:agelabel];

#pragma mark - 得到年龄

-(void)agepicker{

NSDate *select = datapicker.date;

NSTimeInterval dateDiff = [select timeIntervalSinceNow];

int age=-trunc(dateDiff/(60*60*24))/365;//这是根据年月日计算出年龄的一个函数 我的计算出来时是一个负值 直接在前面加了一个负号 也没去管他

NSLog(@"年龄%d",age);

agelabel.text = [NSString stringWithFormat:@"%d",age];

这一是显示具体的年月日

NSDate *select = datapicker.date;

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

[dateFormatter setDateFormat:@"yyyy-MM-dd"];

NSString *dateAndTime = [dateFormatter stringFromDate:select];

label.text = [NSString stringWithFormat:@"%@",dateAndTime];//这是自己做地一个label来显现年月日

}

UIDatePicker 时间滚动表

标签:

原文地址:http://www.cnblogs.com/daijiahong/p/5058795.html

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