Question

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 ?

Was it helpful?

Solution 2

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top