ios - IQKeyboardManager 与 UIDatePicker

标签 ios objective-c iphone xcode6 iqkeyboardmanager

我已经实现了 IQKeyboardManager framework使键盘 handle 更容易。它工作得很好,除了一件事:

我的应用程序中有一些 UItextField 控件可以打开 UIDatePicker 代替默认键盘(例如数字键盘、小数点键盘、支持 ASCII 等) .

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

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

// Assign the datePicker to the textField
[myTextField setInputView:birthdayDatePicker];

我的问题是: 是否可以处理“确定” 按钮上的操作以填充字段“Date de naissance”

enter image description here

编辑:

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

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

    #import "IQDropDownTextField.h"
    
  • .h 中,我将 UITextField 的类型更改为 IQDropDownTextField :

    @property (weak, nonatomic) IBOutlet IQDropDownTextField *myTextField;
    
  • 在 Interface Builder 或 .xib 中选择您的字段,并显示身份检查器:将您的字段类更改为 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 !
  • .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:)];
    
  • .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
    }
    

最佳答案

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

关于ios - IQKeyboardManager 与 UIDatePicker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27926052/

相关文章:

ios - 排序 Mapbox 注释

iphone - 有没有办法获取 NSUserDefaults 中的所有值?

iphone - 使用 AFJSONRequestOperation

iphone - NSNumberFormatter 将 1,000,000 显示为 100 万等

ios - 对类对象使用 objc_setAssociatedObject 是否正确?

ios - MapKit - 更新 MapView 以通过计时器显示移动注释

iPhone - 即使idleTimerDisabled为YES,手机也会进入休眠状态

ios - 为登录用户触发 Segue

iphone - iOS 带通滤波器

ios - 具有重叠单元格和自定义插入和删除动画的 UICollectionView