选择另一个带有UIPickerView错误的UITextField时隐藏键盘

时间:2012-11-05 06:43:00

标签: iphone xcode uitextfield uipickerview

当用户选择UITextField时,我正试图隐藏键盘。我目前有三个文本字段:两个UIPickerView(由同一个选择器控制)和一个textinput字段。当我点击“返回”或在背景上时,键盘会通过resignFirstResponder调用消失。

问题:

当我正在编辑文本输入字段然后立即选择UITextField w / UIPickerView功能时,键盘不会消失。我觉得我已经尝试过所有的解决方案而且正在靠墙撞击......

CODE:

-(void)textFieldDidBeginEditing:(UITextField *)textField{
    [pickerView setHidden:YES];
    if (fldQuiver.editing == YES) {
        [fldTitle resignFirstResponder];
        [fldQuiver resignFirstResponder];
        [pickerView setHidden:NO];
        variabla = 1;
    }else if (fldCategory.editing == YES) {
        [fldTitle resignFirstResponder];
        [fldCategory resignFirstResponder];
        [pickerView setHidden:NO];
        variabla = 2;
    }
    NSLog(@"variabla %d",variabla);
    [pickerView reloadAllComponents];
}

任何帮助将不胜感激。提前谢谢。

王子编辑代码:

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;        // return NO to disallow editing.
{
    //set logic for picker view here
    if (textField == fldQuiver)
    {
        variabla = 1;
    }
    else if (textField == fldCategory) {
        variabla = 2;
    }
    else
    {
    }
    NSLog(@"variabla %d",variabla);
    [pickerView reloadAllComponents];

    if (textField == fldQuiver)
    {
        [fldTitle resignFirstResponder];
        [pickerView setHidden:NO];
        return NO;
    }
    else if (textField == fldCategory) {
        [fldTitle resignFirstResponder];
        [pickerView setHidden:NO];
        return NO;
    }
    else
    {
        [pickerView setHidden:YES];
        return YES;
    }
}

另外:我已经“合成”并宣布@property fldQuiverfldCategory。我IBOutletfldTitlefldQuiver fldCategory

pickerView加载了一个基于变量的数组。

2 个答案:

答案 0 :(得分:2)

使用textFieldShouldBeginEditing delegate方法:

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;        // return NO to disallow editing.
{
   //set logic for picker view here 
   if (textField == fldQuiver) 
   {
      //picker view hidden or show here
      return NO;
   }
   else if (textField == fldCategory) {
       //picker view hidden or show here
      return NO;
   }
   else
   {
      return YES;
   }

}

答案 1 :(得分:0)

为文本字段设置委托

textField.delegate=self;

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
...........
[textField resignFirstResponder];  //it common for all text field,so not use multi resignFirstResponder
............
}