0

I have a view presented modally which contains 3 label.

The third label should not be editable and should show a UIDatePicker when clicked on it, and hiding keyboard if it displayed.

So I did that :

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    if (textField == _birthdateTextField) {

        _birthdateDatePicker.hidden = NO;

        if ([_firstnameTextField isFirstResponder]) {
            NSLog(@"first");
            [_firstnameTextField resignFirstResponder];
        } else if ([_lastnameTextField isFirstResponder]) {
            NSLog(@"last");
            [_lastnameTextField resignFirstResponder];
        }

        return NO;
    }

    _birthdateDatePicker.hidden = YES;

    return YES;
}

I can clearly see my log message ("first" or "last", depending on last focused text field) but the keyboard is still displayed.

Someone is figuring out what's the problem here ?

Yaman
  • 3,949
  • 4
  • 38
  • 60
  • 1
    Not sure if this will help, but you can set your `_birthdateDatePicker` object to the `_birthdateTextField`'s `inputView` property. If you do that, you don't really need to override `textFieldShouldBeginEditing` – Dan F Jan 25 '13 at 19:26
  • Wow thx ! It's working easier like that :D Plz give an answer so i can accept it ;) – Yaman Jan 25 '13 at 19:33

2 Answers2

4

If you presenting modal view as UIModalPresentationFormSheet, answer is here: Modal Dialog Does Not Dismiss Keyboard

iPad keyboard will not dismiss if modal ViewController presentation style is UIModalPresentationFormSheet

Community
  • 1
  • 1
pcholberg
  • 520
  • 2
  • 17
1

Instead of having to do this complicated work with overriding textFieldShouldBeginEditing you can just do this somewhere, like in viewDidLoad:

_birthdateTextField.inputView = _birthdateDatePicker;

And the date picker will replace the keyboard when you go to edit the birthdate text field

Dan F
  • 17,654
  • 5
  • 72
  • 110