3

我想在UITextField结束编辑事件时隐藏键盘,但不知何故我无法让以下代码工作!当我按下完成按钮时,它会隐藏键盘,但当我不按下完成按钮并移动到另一个UITextField我不需要键盘但UIPickerView. 基本上UIPickerView是出现在键盘后面。我UITextField将在结束编辑事件以及开始编辑所需的文本字段时辞职。如果之前没有显示键盘,则开始编辑代码可以正常工作UITextField。有人可以告诉我我做错了什么吗?

以下顺序有效:

  1. 选择正常UITextField并插入文本,按完成按钮(这会隐藏键盘)
  2. 选择选择器UITextField(显示选择器视图)

..但以下没有:

  1. 选择正常UITextField并插入文本
  2. 选择选择器UITextField(选择器视图位于键盘后面,因为我没有按上一个完成按钮UITextField)。这里它调用结束编辑,但它不隐藏键盘!

    - (BOOL)textFieldShouldReturn:(UITextField *)textField {
        [textField resignFirstResponder];
        scrollView.contentSize = CGSizeMake(320, 750);
        [scrollView setFrame:CGRectMake(0, 0, 320, 480)];
        return YES;
     }
    
    -(void)textFieldDidEndEditing:(UITextField *)textField  
    {
        [textField resignFirstResponder];
    }
    
    - (void)textFieldDidBeginEditing:(UITextField *)textField {
        DatePicker.hidden = YES;
        CountryPickerView.hidden = YES;
    
        switch (textField.tag) {
            case 3:
                [textField resignFirstResponder];
                DatePicker.hidden = NO;
                return;
            case 6:
                [textField resignFirstResponder];
                CountryPickerView.hidden = NO;
                return;
            default:
                break;
        }
        scrollView.contentSize = CGSizeMake(320, 650);
        [scrollView setFrame:CGRectMake(0, 0, 320, 260)];
    }
    
4

7 回答 7

10

您不应该依赖标签而是指向对象的指针并[textField resignFirstResponder];从 textFieldDidEndEditing 中删除。

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField 
{ 
    if (textField == theTextFieldIDontWantKeyboardFor) {  
        [thepreviousTextField resignFirstResponder]; 
        return NO;
    }
    return YES; 
}
于 2012-02-28T16:49:33.330 回答
7

它隐藏了键盘,但当我不按完成按钮并移动到另一个我不需要键盘但 PickerView 的 uitextfield 时,它不会隐藏。

处理此问题的正确方法是为inputView使用选择器而不是键盘的字段设置属性。根据需要配置选择器(设置委托、数据源等),然后将其设置为字段的inputView. 当您从一个字段移动到下一个字段时,系统将处理隐藏键盘并显示选择器视图,反之亦然。

于 2012-02-28T20:21:24.733 回答
3
[yourTextField resignFirstResponder];

实际隐藏键盘,所以在选择器即将打开时使用。

于 2012-02-28T16:50:09.677 回答
3

当您将控制从一个文本字段切换到下一个而不辞职时,textFieldDidBeginEditing不会被调用,因此您需要通过将其设为“firstResponder”(因为它不是 firstResponder 由于活动文本字段中的更改)然后调用resignFirstResponder

于 2012-02-28T16:54:58.447 回答
1

试试这个代码

 -(void)textFieldDidEndEditing:(UITextField *)textField  
  {
  if(textField==nameTextField){
    [nameTextField resignFirstResponder];

     //
  }
  else if(textField==pickerTextField){

  ///
  }
  }

  - (BOOL)textFieldShouldReturn:(UITextField *)textField {
  if(textField==nameTextField){
    [nameTextField resignFirstResponder];

    //
  }
  else if(textField==pickerTextField){

  ///
  }
  }
于 2012-02-28T17:00:19.273 回答
0

在这里添加我的最终结果!

Valexa 的解决方案。这工作得很好,但我需要手动处理不同的输入视图。

    - (BOOL)textFieldShouldReturn:(UITextField *)textField {
        [textField resignFirstResponder];
            scrollView.contentSize = CGSizeMake(320, 750);
            [scrollView setFrame:CGRectMake(0, 0, 320, 480)];
        return YES;
    }

    - (void)textFieldDidBeginEditing:(UITextField *)textField {    
           previousTextField = textField;
           scrollView.contentSize = CGSizeMake(320, 650);
           [scrollView setFrame:CGRectMake(0, 0, 320, 260)];
    }

    - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
           if (textField == CountryTextField || textField == BirthdayTextField) {
                  [previousTextField resignFirstResponder]; 
                  return NO; 
           }
          return YES; 
    }

惊人的解决方案(感谢 Caleb):无需监视先前文本字段的额外变量,也无需为显示和添加所需视图而头疼

BirthdayTextField.inputView = DatePickerView;
CountryTextField.inputView = CountryPickerView;
SexTextField.inputView = SexPickerView;

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
    scrollView.contentSize = CGSizeMake(320, 750);
    [scrollView setFrame:CGRectMake(0, 0, 320, 480)];
return YES;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    scrollView.contentSize = CGSizeMake(320, 650);
    [scrollView setFrame:CGRectMake(0, 0, 320, 260)];
}
于 2012-02-28T21:38:15.163 回答
0
-(void)hidekeybord
{
    [_txt_fname resignFirstResponder];
    [_txt_lname resignFirstResponder];
    [_txt_email resignFirstResponder];
    [_txt_phoneN resignFirstResponder];
    [_txt_dateofbd resignFirstResponder];
    [_txt_address resignFirstResponder];
    [_txt_city resignFirstResponder];

}

- (IBAction)btn_open_datepiker:(id)sender
{
    [self hidekeybord];

    _datepiker_bd.hidden=FALSE;
    _toolbar_db.hidden=FALSE;


}
于 2014-05-03T11:54:42.310 回答