gpt4 book ai didi

ios - IQKeyboardManager 与 UIDatePicker

转载 作者:技术小花猫 更新时间:2023-10-29 11:06:45 25 4
gpt4 key购买 nike

我已经实现了 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com