1 #import "ViewController.h"2 3 @interfaceViewcontroller () <UITextFieldDelegate>4@property (Weak, nonatomic) Iboutlet Uitextfield *Birthdaylabel;5@property (Strong, nonatomic) Uidatepicker *DatePicker;6 7 @end8 9 @implementationViewcontrollerTen One- (void) Viewdidload { A [Super Viewdidload]; -_birthdaylabel.Delegate=Self ; - the //setting up a custom keyboard - [self setupbirthdaykeyboard]; - - } + -- (void) Setupbirthdaykeyboard + { A //Create Uidatepicker with default frame, so don't set the size atUidatepicker *picker =[[Uidatepicker alloc] init]; -_datepicker =Picker; - //set localization (native language) -Picker.locale = [Nslocale localewithlocaleidentifier:@"ZH"]; - //set the time display format, there are many other good -Picker.datepickermode =uidatepickermodedate; in - //monitoring the scrolling of Uidatepicker to [Picker addtarget:self action: @selector (Datechange:) forcontrolevents:uicontroleventvaluechanged]; +Self.birthdayLabel.inputView =Picker; - } the *- (void) Datechange: (Uidatepicker *) DatePicker $ {Panax Notoginseng //this will get the datepicker.date of the birthday keyboard. - //NSLog (@ "%@", datepicker.date); the //NSLog (@ "%s", __func__); + A the //converts the acquired date into a string and assigns a value to the Birthdaylabel +NSDateFormatter *fmt =[[NSDateFormatter alloc] init]; -Fmt.dateformat =@"YYYY-MM-DD"; $NSString *datestr =[FMT stringFromDate:datePicker.date]; $_birthdaylabel.text =Datestr; - - } the - //allow editing to startWuyi //-(BOOL) textfieldshouldbeginediting: (Uitextfield *) TextField the //{ - //return NO; Wu //} - //whether the user is allowed to change characters (whether or not to enter text) About-(BOOL) TextField: (Uitextfield *) TextField Shouldchangecharactersinrange: (nsrange) range replacementstring: ( NSString *)string{ $ returnNO; - } -- (void) Textfielddidbeginediting: (Uitextfield *) TextField - { A //Gets the date of the current Datapicker + [self datechange:_datepicker]; the } -- (void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Uievent *)Event $ { the [Self.view Endediting:yes]; the } the @end
Self-learning ideas for developing small features in iOS (pop-up birthday keyboard for example)