deimus
deimus

Reputation: 9865

keyboard is not getting dismissed on UITextField

This may sound a newbie question, however I'm new to iOS dev.

Platform : iPad

I have a UITableView with UITextField, let say they are two. When pressing on the first one virtual keyboard should appear, but when user tapps on the second UITextField the virtual keyboard should be hidden and data picker view should be displayed.

So here it is, how I did it.

-(void) textFieldDidBeginEditing:(UITextField *)textField {
    if (textField.tag == PICKER_VIEW_TAG) {
        [textField resignFirstResponder];
    } else {
        ...
    }
}

-(void) textFieldDidEndEditing:(UITextField *)textField
{
    if (textField.tag != PICKER_VIEW_TAG) {
        ...
    }
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    if (textField.tag == PICKER_VIEW_TAG) {
        [self countriesPickerView];
    }
    return YES;
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (textField.tag == PICKER_VIEW_TAG) {
        [textField resignFirstResponder];
    } else {
        ... 
    }
    return YES;
}

So now the question, when I click for the first time on the first UITextField it displays keyboard, but when I switch to second one it does not hide it. Why ? and how to solve this ?

UPDATE : The corresponding textField is not getting selected but i.e. the resign takes place, right ? but the keyboard is not hidden ... why this happens ?

Upvotes: 2

Views: 2223

Answers (2)

Mark Adams
Mark Adams

Reputation: 30846

Set the inputView property on UITextField to display views other than keyboard to receive input. In this case, you would set the text field's inputView to be an instance of UIDatePicker. The picker will be displayed with the same keyboard animation automatically and you get to delete a bunch of code. Win/win.

Upvotes: 1

Bill Burgess
Bill Burgess

Reputation: 14154

The issue is with your textFieldShouldReturn. If you want it to complete the action, allow it to return while resigning the first responder.

[textField resignFirstResponder];
return YES;

Upvotes: 1

Related Questions