How to develop small functions for iOS (pop-up birthday keyboard is used as an example)
1 # import "ViewController. h "2 3 @ interface ViewController () <UITextFieldDelegate> 4 @ property (weak, nonatomic) IBOutlet UITextField * birthdayLabel; 5 @ property (strong, nonatomic) UIDatePicker * datePicker; 6 7 @ end 8 9 @ implementation ViewController10 11-(void) viewDidLoad {12 [super viewDidLoad]; 13 _ birthdayLabel. delegate = self; 14 15 // set the custom keyboard 16 [self setupBirthdayKeyboard]; 17 18} 19 20-(void) setupBirthdayKeyboard21 {22 // create a UIDatePicker with the default frame, therefore, you do not need to set the size 23 UIDatePicker * picker = [[UIDatePicker alloc] init]; 24 _ datePicker = picker; 25 // set the localization (local language) 26 picker. locale = [NSLocale localeWithLocaleIdentifier: @ "zh"]; 27 // set the time display format. There are many other 28 picker types. datePickerMode = UIDatePickerModeDate; 29 30 // listen to the scroll 31 of UIDatePicker [picker addTarget: self action: @ selector (dateChange :) forControlEvents: UIControlEventValueChanged]; 32 self. birthdayLabel. inputView = picker; 33} 34 35-(void) dateChange :( UIDatePicker *) datePicker36 {37 // to get the datePicker of the birthday keyboard. date38 // NSLog (@ "% @", datePicker. date); 39 // NSLog (@ "% s" ,__ func _); 40 41 42 // convert the obtained date to a string, assigned to birthdayLabel 43 NSDateFormatter * fmt = [[NSDateFormatter alloc] init]; 44 fmt. dateFormat = @ "yyyy-MM-dd"; 45 NSString * datestr = [fmt stringFromDate: datePicker. date]; 46 _ birthdayLabel. text = datestr; 47 48} 49 50 // whether to allow start editing 51 //-(BOOL) textFieldShouldBeginEditing :( UITextField *) textField52 // {53 // return NO; 54 //} 55 // whether the user is allowed to change characters (whether text input is allowed) 56-(BOOL) textField :( UITextField *) textField shouldChangeCharactersInRange :( nsange) range replacementString :( NSString *) string {57 return NO; 58} 59-(void) textFieldDidBeginEditing :( UITextField *) textField60 {61 // obtain the date of the current dataPicker 62 [self dateChange: _ datePicker]; 63} 64-(void) touchesBegan :( NSSet <UITouch *> *) touches withEvent :( UIEvent *) event65 {66 [self. view endEditing: YES]; 67} 68 @ end