美文网首页ios新知识ios 学习iOS 常见功能
优雅的创建一个UIDatePicker输入键盘

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

作者: KevinTing | 来源:发表于2015-11-28 22:01 被阅读909次

前几天在项目中碰到一个这样的需求:需要在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的键盘。下面是头文件:给textField增加一个属性datePickerInput,category是不能直接添加实例变量的,这个datePickerInput会在实现文件里面实现相应的setter和getter。sharedDatePicker类方法是为所有的UITextField生成一个共享的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

相关文章

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

    前几天在项目中碰到一个这样的需求:需要在textField中输入日期,输入的时候弹出datePicker选择日期。...

  • Scanner

    创建Scanner 创建一个键盘输入字符串 创建一个键盘输入整数 键盘输入两个int数字,并且求出和值。 键盘输入...

  • Swift 设置UIDatePicker键盘输入日期

    1、声明datePicker属性 2、设置输入框的键盘为datePicker 3、设置键盘的工具栏 4、添加工具栏...

  • 自定义金额输入键盘

    自定义键盘,用于类似金额输入的情景下,小数点后只能输入两位.自定义键盘的创建:LYQMoneyInputView ...

  • R之数据输入

    键盘输入使用edit()会自动调用一个允许手动输入数据的编辑器 类似于age=numeric(0)的赋值语句将创建...

  • 中二青年逗键盘之iOS开发调戏tableView

    在viewDidLoad中创建输入框 然后监听键盘的弹出 在cellForRow中创建cell且绑定cell的ta...

  • WindowManagerService

    Window 1、创建PhoneWindow2、设置Window的callback,用户的触摸 & 键盘等输入事件...

  • java 键盘输入 与 随机数

    接受键盘输入数据的步骤: (1) 创建一个扫描器对象. (2) 调用扫描器对象的next(), nextInt()...

  • CocoPods使用(备忘)

    从终端进入到项目目录 在项目中创建Profile文件 键盘输入 i,进入编辑模式,输入 然后按Esc,并且输入“ ...

  • UIPickerView

    使用场景 通常在注册模块,通过自定义inputView来自定义键盘加一个UIDatePicker来实现日期的选择 ...

网友评论

    本文标题:优雅的创建一个UIDatePicker输入键盘

    本文链接:https://www.haomeiwen.com/subject/jyodhttx.html