#import"RootViewController.h"@interface Rootviewcontroller () @property (nonatomic, retain) Uitextfield*TextField, @end @implementation rootviewcontroller- (void) viewdidload {[Super viewdidload]; //Customizing the keyboard with DatePicker[self customtextfieldfordatepicker];}#pragmaMark-uidatepicker Date Selector//typically used for custom keyboards- (void) Customtextfieldfordatepicker {/** Create a Uitextfield*/Self.textfield= [[Uitextfield alloc] Initwithframe:cgrectmake (Ten, -, -, +)]; Self.textField.tag= -; Self.textField.borderStyle=Uitextborderstyleroundedrect; [Self.view AddSubview:self.textField]; [Self.textfield release]; /** Create a Button*/UIButton*BTN =[UIButton Buttonwithtype:uibuttontypesystem]; Btn.frame= CGRectMake ( -, Max, -, -); [BTN Settitle:@"Button"Forstate:uicontrolstatenormal]; [Btn addtarget:self Action: @selector (handleaction:) forcontrolevents:uicontroleventtouchupinside]; [Self.view ADDSUBVIEW:BTN]; #pragmaMark-① Initialization method (High is fixed)Uidatepicker*picker =[[Uidatepicker alloc] init]; Picker.tag= $; #pragmaMark-② Setting the display model/*Uidatepickermodetime only displays the time uidatepickermodedate only the date Uidatepickermodedateandtim E Date and time uidatepickermodecountdowntimer hours and minutes*/Picker.datepickermode=uidatepickermodedate; #pragmaMark-③ Set the regional language (set 12/24 hours is displayed according to the phone's own time)Picker.locale= [[Nslocale alloc] Initwithlocaleidentifier:@"ZH_CN"]; #pragmaMark-④ Sets the default calendar (the default is the current day)Picker.calendar=[Nscalendar Currentcalendar]; //Picker.calendar = [[Nscalendar alloc] Initwithcalendaridentifier:nscalendaridentifierchinese]; #pragmaMark-⑤ Set the displayed datepicker.date=[NSDate Date]; [Picker setdate:[nsdate Date] animated:yes]; #pragmaMark-⑥ set time zone (default is 0 time zone)//Picker.timezone = [Nstimezone defaulttimezone];Picker.timezone = [Nstimezone timezonewithname:@"gmt+8"]; NSLog (@"%@", picker.date);#pragmaMark-⑦ min timeNSDateFormatter*fmindate =[[NSDateFormatter alloc] init]; [Fmindate Setdateformat:@"YYYY-MM-DD hh:mm"]; NSDate*mindate = [Fmindate datefromstring:@"2015-09-09 08:20"]; [Fmindate release]; Picker.minimumdate=mindate; #pragmaMark-⑧ Max Timepicker.maximumdate=[NSDate Date];#pragmaMark-⑨ Countdown Duration (model set to Uidatepickermodecountdowntimer)picker.countdownduration=2;#pragmaMark-⑩ Set the minute interval (default is 1, set the interval to be divisible by 60)Picker.minuteinterval=1; #pragmaMark-addtarget:action:..[Picker addtarget:self action: @selector (ChangeValue:) forcontrolevents:uicontroleventvaluechanged]; //Customizing the keyboardSelf.textField.inputView =Picker; [Picker release]; }#pragmaMark-uidatepicker's target Method-(void) ChangeValue: (Uidatepicker *) Picker {//gets the time at which the current uipickerdate is locatedNSDate*date =[Picker Date]; Uitextfield*textfield = (Uitextfield *) [Self.view Viewwithtag: -]; /*//Create date conversion tool NSDateFormatter *formatter = [[NSDateFormatter alloc]init]; Set time zone Formatter.timezone = [Nstimezone timezonewithname:@ "gmt+8"]; Set Date conversion Format Formatter.dateformat = @ "yyyy mm years dd day HH:MM:SS"; Textfield.text = [Formatter stringfromdate:date];*/Textfield.text= [NSString stringWithFormat:@"%@", date];}#pragmaMark-Method-(void) Handleaction: (UIButton *) Sender {//Get UidatepickerUidatepicker *picker = (Uidatepicker *) Self.textField.inputView; //Set Date-(void) SetDate: (NSDate *) Date animated: (BOOL) animated; //[picker setdate:[nsdate Date] animated:no];Picker.date =[NSDate Date]; //Refresh Data[self changevalue:picker];} //end of Edit, recycle keyboard- (void) touchesended: (Nsset *) touches withevent: (Uievent *)Event{[Self.view endediting:yes];} /*Nscalendar Set calendar + (ID) Currentcalendar; Gets the current user's logical calendar (logical calendar) + (ID) Autoupdatingcurrentcalendar; Get the current user's logical calendar (logical calendar), ...-(ID) Initwithcalendaridentifier: (NSString *) identifier; Initialize to various calendars. The range of identifier can be: Nscalendaridentifiergregorian Gregorian nscalendaridentifierbuddhist Buddhist calendar Nscalendaridentifierchinese China Calendar Nscalendaridentifierhebrew Hebrew Calendar nscalendaridentifierislamic Islamic Calendar nscalendaridentifierislamiccivil Islamic civil calendar Nscalendaridentifierjapanese Japan Calendar*/@end
Uidatepicker Date Selector in iOS