Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IQKeyboardManager with UIDatePicker

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" ?

enter image description here

EDIT :

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.

Note according to Mohd Iftekhar Qurashi comment : the two next points can be avoided with the following code :

// 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
    }
    
like image 847
Jonathan F. Avatar asked Jan 13 '15 15:01

Jonathan F.


1 Answers

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'.

like image 89
Mohd Iftekhar Qurashi Avatar answered Nov 09 '22 06:11

Mohd Iftekhar Qurashi