当点击另一个引出选择器的UITextField时,键盘不会消失

时间:2013-04-19 13:41:06

标签: iphone ios objective-c uipickerview uipickerviewcontroller

我有两个文本字段,我在每个文本字段中点击时打开一个带有工具栏的拾取轮,顶部提供了解除选择器并调出键盘的选项一切正常,除非你解雇选择器并带上键盘然后单击下一个文本字段。我把键盘放在顶部,后面有新的pickerview。让键盘消失的唯一方法是在第一个文本字段中单击并单击“完成”或屏幕上的任何位置(而不是文本字段)。

这是我的代码:

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    for (NSUInteger i = 0; i<[self.fieldsArray count]; i++) {
        if ([self.fieldsArray objectAtIndex:i] == textField) {
            UITextField *input = [self.fieldsArray objectAtIndex:i];

            if (input.tag == 3 && !self.overrideDriver) {
                [self animatePickDriverForInput:input];
            }
            if (input.tag == 4 && !self.overrideVehicle) {
                [self animatePickVehicleForInput:input];
            }

        }
    }
}

以下是其他一些使用的方法:

- (IBAction)textFieldFinished:(id)sender
{
    [sender resignFirstResponder];
}


- (BOOL)textFieldShouldReturn:(UITextField *)textField {

    [textField resignFirstResponder];

    return YES;

}

- (void)animatePickDriverForInput:(UITextField *)input
{
    if ([self.drivers count] > 0) {
        [self.view endEditing:YES];
        [self showPickDriver];
    } else {
        //untested
        [input addTarget:self action:@selector(textFieldFinished:)
            forControlEvents:UIControlEventEditingDidEndOnExit];
    }
}


- (void)animatePickVehicleForInput:(UITextField *)input
{
    if ([self.vehicles count] > 0) {
        [self.view endEditing:YES];
        [self showPickVehicle];
    } else {
        //untested
        [input addTarget:self action:@selector(textFieldFinished:)
            forControlEvents:UIControlEventEditingDidEndOnExit];
    }
}

- (void)allowManualEntryOfDriver
{
    [self.additionalButtonPickerHelper animateDismiss:self.pickDriver];
    self.overrideDriver = YES;
    [self.driver becomeFirstResponder];
}

- (void)allowManualEntryOfVehicle
{
    [self.additionalButtonPickerHelper animateDismiss:self.pickVehicle];
    self.overrideVehicle = YES;
    [self.vehicle becomeFirstResponder];
}



- (void)showPickVehicle {
    self.pickVehicle = [self.additionalButtonPickerHelper createPicker:self.pickVehicle WithTag:2 WithOtherButtonText:@"Add Other Vehicle"];
    [self.additionalButtonPickerHelper showPicker:self.pickVehicle WithDoneAction:@selector(dismissVehiclePicker) OrWithOtherAction:@selector(allowManualEntryOfVehicle)];
}

- (void)showPickDriver {
    self.pickDriver = [self.additionalButtonPickerHelper createPicker:self.pickDriver WithTag:1 WithOtherButtonText:@"Add Other Driver"];
    [self.additionalButtonPickerHelper showPicker:self.pickDriver WithDoneAction:@selector(dismissDriverPicker) OrWithOtherAction:@selector(allowManualEntryOfDriver)];
}

编辑:

更多代码:

- (void)dismissDriverPicker
{
    [self.additionalButtonPickerHelper animateDismiss:self.pickDriver];
}

- (void)dismissVehiclePicker
{
    [self.additionalButtonPickerHelper animateDismiss:self.pickVehicle];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

这可能不是最有效的方法,但我是目标c的新手,并寻找一个我能理解的解决方案。谢谢!

在这里编辑的是图片keyboard being stuck

5 个答案:

答案 0 :(得分:0)

请尝试使用此

   - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
    {
        [self showPickerView];
        return NO;
    }

    - (void)showPickerView
    {
       // ----- here create your picker view --------
    }

并且您可以使用第二种方式,但这不是正确的方式...所以我说更喜欢第一种方法。

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
   [self.view endEditing:YES];
}

答案 1 :(得分:0)

为了得到这样你可以使用这个

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    [self showPickDriver];

    return NO;
}

否则最好使用带有水龙头手势的标签,那么你可以轻松解决这个问题。

答案 2 :(得分:0)

此外,您可以尝试使用textField的属性: inputView

  

文本字段成为第一个时显示的自定义输入视图   响应者。

因此,您可以显示此视图而不是键盘,并且不会手动处理它。

现在出现错误的行为,因为还显示了第二个文本字段的键盘。因此,您不仅需要辞退第一个字段的第一响应者,还需要实现:

-(BOOL) textFieldShouldBeginEditing:(UITextField *)field {
    return NO;
}

答案 3 :(得分:0)

UITextField包含inputViewinputAccessoryView属性,可以解决这种情况:

  

<强> inputView

     

如果此属性中的值为nil,则文本字段在成为第一响应者时显示标准系统键盘。为此属性分配自定义视图会导致显示该视图。

答案 4 :(得分:0)

如果包含textfield的视图控制器显示为 modalViewController 并带有表单样式,则必须覆盖以下给定的方法才能使其正常工作。

- (BOOL)disablesAutomaticKeyboardDismissal 
{
   return NO;
}