0

当用户选择UITextField. 我目前有三个文本字段:两个UIPickerView(由同一个选择器控制)和一个文本输入字段。当我单击“返回”或在后台单击时,键盘通过resignFirstResponder呼叫消失。

问题:

当我当前正在编辑文本输入字段然后立即选择UITextFieldw/UIPickerView功能时,键盘不会消失。我觉得我已经尝试了所有解决方案,并且正在用头撞墙......

代码:

-(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 fldQuiverand fldCategory。我有IBOutlet, fldTitle, fldQuiver.fldCategory

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

4

2 回答 2

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

}
于 2012-11-05T07:14:20.933 回答
0

为文本字段设置委托

textField.delegate=self;

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
...........
[textField resignFirstResponder];  //it common for all text field,so not use multi resignFirstResponder
............
}
于 2012-11-05T07:00:23.600 回答