iOS開發小功能的自學思路(彈出生日鍵盤為例),ios思路

來源:互聯網
上載者:User

iOS開發小功能的自學思路(彈出生日鍵盤為例),ios思路

 

 

 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     //    設定自訂鍵盤16         [self setupBirthdayKeyboard];17         18     }19     20     - (void)setupBirthdayKeyboard21     {22     //    建立UIDatePicker,有預設的frame,所以不用設定尺寸23         UIDatePicker *picker = [[UIDatePicker alloc] init];24         _datePicker = picker;25     //    設定本地化(本地語言)26         picker.locale = [NSLocale localeWithLocaleIdentifier:@"zh"];27     //    設定時間顯示格式,還有其他好多種28         picker.datePickerMode = UIDatePickerModeDate;29        30         //監聽UIDatePicker的滾動31         [picker addTarget:self action:@selector(dateChange:) forControlEvents:UIControlEventValueChanged];32          self.birthdayLabel.inputView = picker;33     }34     35     - (void)dateChange:(UIDatePicker *)datePicker36     {37     //    這樣就可以獲得生日鍵盤的  datePicker.date38     //    NSLog(@"%@",datePicker.date);39     //    NSLog(@"%s",__func__);40         41         42         //把獲得的日期轉化成字串,賦值到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     //是否允許開始編輯51     //- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField52     //{53     //    return NO;54     //}55     //是否允許使用者改變字元(是否允許輸入文字)56     - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{57         return NO;58     }59     - (void)textFieldDidBeginEditing:(UITextField *)textField60     {61         //擷取當前dataPicker的日期62          [self dateChange:_datePicker];63     }64     - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event65     {66         [self.view endEditing:YES];67     }68     @end

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.