iphoneiosobjective-cuipickerviewuipickerviewcontroller

Keyboard not dismissing when another UITextField is tapped that brings up a picker


I have 2 text fields that I am working with each one when clicked opens up a picker wheel with a toolbar on top that gives the option to dismiss the picker and bring up a keyboard everything works fine unless you dismiss the picker and bring up the keyboard then click the next textfield. I get the keyboard on top with the new pickerview behind it. And the only way to get the keyboard to go away is to click back in the first textfield and click done or anywhere on the screen (not a textfield).

here is my code:

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    for (NSUInteger i = 0; i<[self.fieldsArray count]; i++) {
        if ([self.fieldsArray objectAtIndex:i] == textField) {
            UITextField *input = [self.fieldsArray objectAtIndex:i];

            if (input.tag == 3 && !self.overrideDriver) {
                [self animatePickDriverForInput:input];
            }
            if (input.tag == 4 && !self.overrideVehicle) {
                [self animatePickVehicleForInput:input];
            }

        }
    }
}

Here are some other methods used:

- (IBAction)textFieldFinished:(id)sender
{
    [sender resignFirstResponder];
}


- (BOOL)textFieldShouldReturn:(UITextField *)textField {

    [textField resignFirstResponder];

    return YES;

}

- (void)animatePickDriverForInput:(UITextField *)input
{
    if ([self.drivers count] > 0) {
        [self.view endEditing:YES];
        [self showPickDriver];
    } else {
        //untested
        [input addTarget:self action:@selector(textFieldFinished:)
            forControlEvents:UIControlEventEditingDidEndOnExit];
    }
}


- (void)animatePickVehicleForInput:(UITextField *)input
{
    if ([self.vehicles count] > 0) {
        [self.view endEditing:YES];
        [self showPickVehicle];
    } else {
        //untested
        [input addTarget:self action:@selector(textFieldFinished:)
            forControlEvents:UIControlEventEditingDidEndOnExit];
    }
}

- (void)allowManualEntryOfDriver
{
    [self.additionalButtonPickerHelper animateDismiss:self.pickDriver];
    self.overrideDriver = YES;
    [self.driver becomeFirstResponder];
}

- (void)allowManualEntryOfVehicle
{
    [self.additionalButtonPickerHelper animateDismiss:self.pickVehicle];
    self.overrideVehicle = YES;
    [self.vehicle becomeFirstResponder];
}



- (void)showPickVehicle {
    self.pickVehicle = [self.additionalButtonPickerHelper createPicker:self.pickVehicle WithTag:2 WithOtherButtonText:@"Add Other Vehicle"];
    [self.additionalButtonPickerHelper showPicker:self.pickVehicle WithDoneAction:@selector(dismissVehiclePicker) OrWithOtherAction:@selector(allowManualEntryOfVehicle)];
}

- (void)showPickDriver {
    self.pickDriver = [self.additionalButtonPickerHelper createPicker:self.pickDriver WithTag:1 WithOtherButtonText:@"Add Other Driver"];
    [self.additionalButtonPickerHelper showPicker:self.pickDriver WithDoneAction:@selector(dismissDriverPicker) OrWithOtherAction:@selector(allowManualEntryOfDriver)];
}

edit:

More code:

- (void)dismissDriverPicker
{
    [self.additionalButtonPickerHelper animateDismiss:self.pickDriver];
}

- (void)dismissVehiclePicker
{
    [self.additionalButtonPickerHelper animateDismiss:self.pickVehicle];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

This may not be the most efficient way to do this but im new to objective c and looking for a solution that I can understand. Thanks!

edit here is a picture keyboard being stuck


Solution

  • Also you can try to use property of textField : inputView

    The custom input view to display when the text field becomes the first responder.

    So, you can show this view instead of keyboard, and don't handle it manually.

    Or:

    Wrong behavior for now occur, because keyboard for second textfield is shown also. So you need not only to resign first responder of first field, but also implement:

    -(BOOL) textFieldShouldBeginEditing:(UITextField *)field {
        return NO;
    }