带有UIDatePicker的IQKeyboardManager

Erz*_*iel 10 iphone objective-c ios xcode6 iqkeyboardmanager

我已经实现了IQKeyboardManager框架,使键盘手柄更容易.它的工作非常精细,除了一件事:

UItextField在我的应用程序中有一些控件打开UIDatePicker代替默认键盘(例如数字键盘,小数点,ASCII能力等).

这是一个带有图形结果的代码示例:

// Create the datePicker
UIDatePicker *birthdayDatePicker = [UIDatePicker new];
[birthdayDatePicker setDatePickerMode:UIDatePickerModeDate];

// Assign the datePicker to the textField
[myTextField setInputView:birthdayDatePicker];
Run Code Online (Sandbox Code Playgroud)

我的问题是: 是否可以通过"确定"按钮处理操作以填写"日期复兴"字段?

在此输入图像描述

编辑:

对于那些想知道我是如何解决问题的人:

  • 在我的.h中,我输入了IQDropDownTextField.h:

    #import "IQDropDownTextField.h"
    
    Run Code Online (Sandbox Code Playgroud)
  • .h中,我将我的类型更改UITextFieldIQDropDownTextField:

    @property (weak, nonatomic) IBOutlet IQDropDownTextField *myTextField;
    
    Run Code Online (Sandbox Code Playgroud)
  • 在Interface Builder或.xib中选择您的字段,并显示Identity Inspector:将您的字段类更改为IQDropDownTextField.

请注意Mohd Iftekhar Qurashi评论:使用以下代码可以避免以下两点:

// Set myTextField's dropDownMode to IQDropDownModeDatePicker
myTextField.dropDownMode = IQDropDownModeDatePicker;

// Create a dateFormatter
NSDateFormatter *df = [NSDateFormatter new];
[df setDateFormat:@"dd/MM/yyyy"];

// Assign the previously created dateFormatter to myTextField
myTextField.dateFormatter = df;

// Assign a minimum date and/or maximum date if you want
myTextField.minimumDate = [NSDate date];
myTextField.maximumDate = [NSDate date];

// That's all !
Run Code Online (Sandbox Code Playgroud)
  • .m中,我添加了setCustomDoneTarget:action:方法:

    // Create the datePicker
    UIDatePicker *birthdayDatePicker = [UIDatePicker new];
    [birthdayDatePicker setDatePickerMode:UIDatePickerModeDate];
    
    // Assign the datePicker to the textField
    [myTextField setInputView:birthdayDatePicker];
    
    // Just added this line
    [myTextField setCustomDoneTarget:self action:@selector(doneAction:)];
    
    Run Code Online (Sandbox Code Playgroud)
  • .m中,我添加了doneAction:方法:

    - (void)doneAction:(UITextField *)textField
    {
        [myTextField setText:[DateHelper getStringFromDate:birthdayDatePicker.date format:@"dd/MM/yyyy" useGmt:NO]]; // getStringFromDate:format:useGmt: is a method to convert a NSDate to a NSString according to the date format I want
    }
    
    Run Code Online (Sandbox Code Playgroud)

Moh*_*shi 3

您现在可以添加自定义选择器(请参阅'IQUIView+IQKeyboardToolbar.h')以previous/next/done获取通知。请注意,自定义选择器不会影响 的本机功能previous/next/done,它仅用于回调目的。有关详细文档'IQUIView+IQKeyboardToolbar.h',请参阅“如何使用?” 请参考“ TextFieldViewController.m”。