nsexception - ERROR SHOWING in program code ios -
-(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
try select date date picker...
-(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]; }
to calculate date....
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);
Comments
Post a Comment