错误显示在程序代码ios中

时间:2015-04-29 04:24:20

标签: ios nsexception nsdatepicker date-of-birth

 -(void)textFieldDidBeginEditing:(UITextField *)textField
 {

    [textField resignFirstResponder];



    picker   = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 290, 320, 216)];



    [picker setDatePickerMode:UIDatePickerModeDate];

   picker.backgroundColor = [UIColor redColor];

    self.txt_dob.inputView = picker;

   [picker setMaximumDate:[NSDate date]];

    format=[[NSDateFormatter alloc]init];

    [format setDateFormat:@"dd/MM/YYYY"];

    [self.view addSubview:picker];

    UIToolbar *toolBar= [[UIToolbar alloc] initWithFrame:CGRectMake(0,0,320,44)];

    [toolBar setBarStyle:UIBarStyleBlackOpaque];

    UIBarButtonItem *barButtonDone = [[UIBarButtonItem alloc] initWithTitle:@"Done"                                                 style:UIBarButtonItemStyleBordered target:self action:@selector(showSelectedDate:)];

    toolBar.items = @[barButtonDone];

    barButtonDone.tintColor=[UIColor blackColor];

    [picker addSubview:toolBar];

}

{

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

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

NSDate *startD = [dateFormatter dateFromString:@"15-Sep-1997"];

NSDate *endD = [NSDate date];

 NSCalendar *calendar = [NSCalendar currentCalendar];

//NSUInteger unitFlags = NSCalendarUnitYear|NSCalendarUnitMonth|//NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond;

NSDateComponents *components = [calendar components:unitFlags fromDate:startD toDate:endD options:0];

NSInteger year  = [components year];

NSInteger month  = [components month];

NSInteger day  = [components day];

NSLog(@"%ld:%ld:%ld", (long)year, (long)month,(long)day);
}

@end

1 个答案:

答案 0 :(得分:0)

尝试此选择从日期选择器中选择日期...

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
datePicker =[[UIDatePicker alloc]initWithFrame:CGRectMake(50,150,10, 50)];
datePicker.datePickerMode=UIDatePickerModeDate;
datePicker.hidden=NO;
datePicker.date=[NSDate date];
[datePicker addTarget:self action:@selector(textFieldTitle:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:datePicker];
UIBarButtonItem * rightBtn=[[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(save:)];
self.navigationItem.rightBarButtonItem=rightBtn;
return true;
}


-(void)textFieldTitle:(id)sender

{
NSDateFormatter *dateFormat=[[NSDateFormatter alloc]init];
dateFormat.dateStyle=NSDateFormatterMediumStyle;
[dateFormat setDateFormat:@"MM/dd/yyyy"];
NSString *str=[NSString stringWithFormat:@"%@",[dateFormat  stringFromDate:datePicker.date]];
self.txtField.text=str;
}

-(void)save:(id)sender
{
self.navigationItem.rightBarButtonItem=nil;
[datePicker removeFromSuperview];

}

计算日期......

NSDate *today = [NSDate date];
NSString *birthdate=self.txtField.text;
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setDateFormat:@"dd-MM-yyyy"];
NSDate *myDate = [[NSDate alloc]init];
myDate=[dateFormatter1 dateFromString:birthdate];


NSDateComponents *ageComponents = [[NSCalendar currentCalendar]
                                   components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay
                                   fromDate:myDate
                                   toDate:today
                                   options:0];

NSLog(@"%@",ageComponents);