10

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
    }
    
Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223
Jonathan F.
  • 2,357
  • 4
  • 26
  • 44
  • FYI, IQDropDownTextField have property to set it's 'IQDropDownMode', you can directly set it's mode to 'IQDropDownModeDatePicker' to show datePicker as inputView. No need to maintain datePicker manually, get the selected date from 'date' property. – Mohd Iftekhar Qurashi Jan 16 '15 at 17:36
  • @MohdIftekharQurashi Thanks for the information, I missed it. It saves me a few lines of code. – Jonathan F. Jan 19 '15 at 08:38

1 Answers1

3

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

MAhipal Singh
  • 4,745
  • 1
  • 42
  • 57
Mohd Iftekhar Qurashi
  • 2,385
  • 3
  • 32
  • 44