您的位置:首页 > 产品设计 > UI/UE

UIDatePicker 时间滚动表

2015-12-19 11:12 453 查看
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来显现年月日
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: