Question

I have 4 textfields, when editing 3 of them I want the standard keyboard which works fine, but on the 4th textfield i want to display a picker wheel instead.

My code works fine when pressing on the 4th textfield directly, but if I edit textfield 1-3 and then press on number 4, the keyboard doesn't remove.

Any ideas why? and how to solve it?

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
   if ([textField.text isEqualToString:selectedCountryText])
   {
     NSLog(@"Edit country");
     [self.view endEditing:YES];

     [UIView beginAnimations: @"anim" context: nil];
     [UIView setAnimationBeginsFromCurrentState: YES];
     [UIView setAnimationDuration: 0.3f];
     if (pickerVisible)
     {
        picker.frame = CGRectOffset(picker.frame, 0, -341);
        pickerVisible = FALSE;
     }
     else
     {
        picker.frame = CGRectOffset(picker.frame, 0, 341);
        pickerVisible = TRUE;
     }
     [UIView commitAnimations];
     [self removeKeyboard];
  }
  else
  {
    if (pickerVisible)
    {
        //Remove picker
        [UIView beginAnimations: @"anim" context: nil];
        [UIView setAnimationBeginsFromCurrentState: YES];
        [UIView setAnimationDuration: 0.3f];
        picker.frame = CGRectOffset(picker.frame, 0, -341);
        [UIView commitAnimations];
        pickerVisible = FALSE;
    }
  }       
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
  if ([textField.text isEqualToString:selectedCountryText])
  {
    //do nothing for now
  }
  else
  {
    [self.view endEditing:YES];
    [self removeKeyboard];
  }
}

- (void) removeKeyboard
{
    [self.firstname resignFirstResponder];
    [self.lastname resignFirstResponder];
    [self.year resignFirstResponder];
    [self.country resignFirstResponder];
}

In The view did load I have set the delegate.

- (void)viewDidLoad
{
  NSLog(@"viewdidload SearchSwimmerView");
  // Setup the text fields..
  [self.firstname setDelegate:self];
  [self.firstname setReturnKeyType:UIReturnKeyDone];
  [self.firstname addTarget:self action:@selector(removeKeyboard) forControlEvents:UIControlEventEditingDidEndOnExit];

  [self.lastname setDelegate:self];
  [self.lastname setReturnKeyType:UIReturnKeyDone];
  [self.lastname addTarget:self action:@selector(removeKeyboard) forControlEvents:UIControlEventEditingDidEndOnExit];

  [self.year setDelegate:self];
  [self.year setReturnKeyType:UIReturnKeyDone];
  [self.year addTarget:self action:@selector(removeKeyboard) forControlEvents:UIControlEventEditingDidEndOnExit];

  [self.country setDelegate:self];
}
Was it helpful?

Solution 2

I think setting inputView for 4th textfield as PickerView may do the trick.

textfield.inputView = pickerView;

OTHER TIPS

Instead of using [self.view endEditing:YES] Use

    [textField performSelector:@selector(resignFirstResponder)
                    withObject:nil
                    afterDelay:0];
- (void)textFieldDidBeginEditing:(UITextField *)textField
{

  if(textField==fourthtextfield)
  {
     return NO;
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top