编辑其他文本字段时隐藏键盘

时间:2012-12-20 10:16:18

标签: ios uitextfield uipickerview uikeyboard uitextfielddelegate

我有4个文本字段,当编辑其中3个时我想要标准键盘工作正常,但在第4个文本字段我想显示一个选择器轮。

直接按下第4个文本字段时,我的代码工作正常,但如果我编辑文本字段1-3然后按数字4,则键盘不会删除。

任何想法为什么?以及如何解决它?

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

在视图中加载我已设置委托。

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

3 个答案:

答案 0 :(得分:2)

而不是使用[self.view endEditing:YES]使用

    [textField performSelector:@selector(resignFirstResponder)
                    withObject:nil
                    afterDelay:0];

答案 1 :(得分:0)

- (void)textFieldDidBeginEditing:(UITextField *)textField
{

  if(textField==fourthtextfield)
  {
     return NO;
  }
}

答案 2 :(得分:0)

我认为将第四个文本字段的inputView设置为PickerView可能会有所作为。

textfield.inputView = pickerView;