How to develop small functions for iOS (pop-up birthday keyboard is used as an example)

Source: Internet
Author: User

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

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.