I've implemented the IQKeyboardManager framework to make the keyboard handle easier. It works very fine, except for one thing :
There're some UItextField
controls in my app which open a UIDatePicker
in place of a default keyboard (e.g. number pad, decimal pad, ASCII capable, etc.).
Here's a code sample with the graphical result :
// Create the datePicker
UIDatePicker *birthdayDatePicker = [UIDatePicker new];
[birthdayDatePicker setDatePickerMode:UIDatePickerModeDate];
// Assign the datePicker to the textField
[myTextField setInputView:birthdayDatePicker];
My question is : Is it possible to handle the action on the "OK" button to fill the field "Date de naissance" ?
For the ones who want to know how I solved my problem :
in my .h, I imported IQDropDownTextField.h
:
#import "IQDropDownTextField.h"
in the .h, I changed the type of my UITextField
to IQDropDownTextField
:
@property (weak, nonatomic) IBOutlet IQDropDownTextField *myTextField;
select your field in Interface Builder or in your .xib, and show the Identity Inspector : change your field's class to IQDropDownTextField
.
// 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 !
in the .m, I added the setCustomDoneTarget:action:
method :
// 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:)];
in the .m, I added the doneAction:
method :
- (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
}
You can now add customised selector(please refer 'IQUIView+IQKeyboardToolbar.h'
) for previous/next/done
to get notify. Note that custom selector doesn't affect the native functionality of previous/next/done
, it's just used for callback purpose only. For detail documentation please refer 'IQUIView+IQKeyboardToolbar.h'
, for 'how to use?' please refer 'TextFieldViewController.m
'.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With