星空网 > 软件开发 > 操作系统

iOS开发小功能的自学思路(弹出生日键盘为例)

iOS开发小功能的自学思路(弹出生日键盘为例)images/loading.gif' data-original="http://images2015.cnblogs.com/blog/938268/201604/938268-20160421075107960-1355367210.png" width="189" height="331" />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

 




原标题:iOS开发小功能的自学思路(弹出生日键盘为例)

关键词:IOS

IOS
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

tiktok国内登录:https://www.goluckyvip.com/tag/83955.html
tiktok播放为0:https://www.goluckyvip.com/tag/83956.html
tiktok挂v:https://www.goluckyvip.com/tag/83957.html
采集tiktok:https://www.goluckyvip.com/tag/83958.html
tiktok登录显示没网:https://www.goluckyvip.com/tag/83959.html
延迟计划:https://www.goluckyvip.com/tag/8396.html
斥资210万美元,TikTok游说美国参议院阻止法案:https://www.goluckyvip.com/news/188213.html
大家推荐一下厦门三整天一个半天的旅游攻略:https://www.vstour.cn/a/365181.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流