【问题标题】:IQKeyboardManager with UIDatePickerIQKeyboardManager 与 UIDatePicker
【发布时间】:2015-01-13 15:50:35
【问题描述】:

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

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

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

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

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

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

编辑:

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

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

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

    @property (weak, nonatomic) IBOutlet IQDropDownTextField *myTextField;
    
  • 在 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 !
  • .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
    }
    

【问题讨论】:

  • 仅供参考,IQDropDownTextField 有属性可以设置它的 'IQDropDownMode',你可以直接将它的模式设置为 'IQDropDownModeDatePicker' 以将 datePicker 显示为 inputView。无需手动维护 datePicker,从 'date' 属性中获取选定的日期。
  • @MohdIftekharQurashi 感谢您提供的信息,我错过了。它为我节省了几行代码。

标签: ios objective-c iphone xcode6 iqkeyboardmanager


【解决方案1】:

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

【讨论】:

  • Pod 更新了吗?我运行了pod update,但我没有在您的最新提交中获得更新(例如setCustomDoneTarget:action: 方法)。
  • 我昨天实现了这些选择器,pod还没有更新,但是github repo已经更新了。
  • 好的。无论如何感谢您的快速实施!非常棒的框架。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-19
  • 2018-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多