Question

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

Was it helpful?

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;
}

OTHER TIPS

for getting like this you can use this one

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    [self showPickDriver];

    return NO;
}

or

otherwise better to use labels with tap Gestures then you can solve this problem easily.

please try to use this one

   - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
    {
        [self showPickerView];
        return NO;
    }

    - (void)showPickerView
    {
       // ----- here create your picker view --------
    }

and you can use second one way but this is not proper way...so i say to prefer first one method.

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

UITextField includes the inputView and inputAccessoryView properties to address this very situation:

inputView

If the value in this property is nil, the text field displays the standard system keyboard when it becomes first responder. Assigning a custom view to this property causes that view to be presented instead.

if the view controller containing the textfield is presented as modalViewController with formsheet style, then you have to override the below given method to make it work properly.

- (BOOL)disablesAutomaticKeyboardDismissal 
{
   return NO;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top