优雅的创建一个UIDatePicker输入键盘

weipeng2k 9年前

来自: http://www.cocoachina.com/ios/20160212/15246.html

作者: KevinTing

前几天在项目中碰到一个这样的需求:需要在textField中输入日期,输入的时候弹出datePicker选择日期。心想很简单啊,无非就是把textField的inputView属性设置为UIDatePicker的实例即可啊。然后在UIDatePicker的pickerValueChanged事件中更新textField的值就可以了啊。我开始是这么做的:

- (void)viewDidLoad {      [super viewDidLoad];      // Do any additional setup after loading the view, typically from a nib.            UIDatePicker *picker = [[UIDatePicker alloc] init];      picker.datePickerMode = UIDatePickerModeDate;      [picker addTarget:self action:@selector(pickerValueChanged:) forControlEvents:UIControlEventValueChanged];      self.firstTextField.inputView = picker;  }    - (void)pickerValueChanged:(UIDatePicker *)sender  {      NSDateFormatter *formatter = [[NSDateFormatter alloc] init];      [formatter setDateFormat:@"yyyy-MM-dd"];      self.firstTextField.text = [formatter stringFromDate:sender.date];  }

恩,运行的很好。等等,老板来了说一句,这么快搞好了啊,那这个view上面再加几个日期输入框吧。客户查询起始时间,还要有结束时间啊。好吧,这个也不难的,拉一个textField。如法炮制,在viewDidLoad屁股后面加上:

self.secondTextField.inputView = picker;

然后为了避免输入冲突,在textField的delegate方法中记录当前输入框:

- (void)textFieldDidBeginEditing:(UITextField *)textField  {      self.currentDateTextField = textField;  }

并修改pickerValueChanged方法。

- (void)pickerValueChanged:(UIDatePicker *)sender  {      NSDateFormatter *formatter = [[NSDateFormatter alloc] init];      [formatter setDateFormat:@"yyyy-MM-dd"];      self.currentDateTextField.text = [formatter stringFromDate:sender.date];  }

ok了。但是,后面做的过程中发现这个界面不只有输入日期的textField,还有其他的普通textField啊。那只能修改currentDateTextField的记录条件了。

- (void)textFieldDidBeginEditing:(UITextField *)textField  {      if (textField == self.firstTextField ||          textField == self.secondTextField)      {          self.currentDateTextField = textField;      }  }

好的,这个界面搞定了,老板又来说。后面有几个界面也是这样的,需要加几个日期选择输入框。妈蛋啊!那不是又得再其他的viewController中加入这些代码,设置delegate,实现delegate方法。实话说,这代码量也不大啊。但是这个代码放到viewController中,一是无法复用,二是这样写好怂 啊!代码都是一样的,应该抽出来复用啊!

于是,我写了一个textField的category,直接用一个属性datePickerInput来设置UIDatePicker的键盘。下面是头文件:

#import [UIKit/UIKit.h](因识别问题,这里将尖括号改为方括号)    @interface UITextField (DatePicker)    @property (nonatomic, assign) BOOL datePickerInput;    + (UIDatePicker *)sharedDatePicker;    @end

下面是m文件:

#import "UITextField+DatePicker.h"    @implementation UITextField (DatePicker)    // 1  + (UIDatePicker *)sharedDatePicker;  {      static UIDatePicker *daterPicker = nil;      static dispatch_once_t onceToken;      dispatch_once(&onceToken, ^{          daterPicker = [[UIDatePicker alloc] init];          daterPicker.datePickerMode = UIDatePickerModeDate;      });            return daterPicker;        }    // 2  - (void)datePickerValueChanged:(UIDatePicker *)sender  {      if (self.isFirstResponder)      {          NSDateFormatter *formatter = [[NSDateFormatter alloc] init];          [formatter setDateFormat:@"yyyy-MM-dd"];          self.text = [formatter stringFromDate:sender.date];      }  }    // 3  - (void)setDatePickerInput:(BOOL)datePickerInput  {      if (datePickerInput)      {          self.inputView = [UITextField sharedDatePicker];          [[UITextField sharedDatePicker] addTarget:self action:@selector(datePickerValueChanged:) forControlEvents:UIControlEventValueChanged];      }      else      {          self.inputView = nil;          [[UITextField sharedDatePicker] removeTarget:self action:@selector(datePickerValueChanged:) forControlEvents:UIControlEventValueChanged];      }  }    // 4  - (BOOL)datePickerInput  {      return [self.inputView isKindOfClass:[UIDatePicker class]];  }  end

代码很简单,1处是套用单例的写法初始化一个全局的datePicker;2处设置text,此处必须判断是不是isFirstResponder;3和4是setter和getter。

后面再加上日期选择输入框的时候,只需要这样写就行了:

self.firstTextField.datePickerInput = YES;  self.secondTextField.datePickerInput = YES;

恩,比之前好多了!

完整的demo项目在这里: https://github.com/tujinqiu/KTTextFieldDatePickerKeyBoard