简体   繁体   English

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

[英]Keyboard not dismissing when another UITextField is tapped that brings up a picker

I have 2 text fields that I am working with each one when clicked opens up a picker wheel with a toolbar on top that gives the option to dismiss the picker and bring up a keyboard everything works fine unless you dismiss the picker and bring up the keyboard then click the next textfield. 我有2个文本字段,我在每个文本字段中点击时打开一个带有工具栏的拾取轮,顶部提供了解除选择器并调出键盘的选项一切正常,除非你关闭选择器并调出键盘然后单击下一个文本字段。 I get the keyboard on top with the new pickerview behind it. 我把键盘放在顶部,后面有新的pickerview。 And the only way to get the keyboard to go away is to click back in the first textfield and click done or anywhere on the screen (not a textfield). 让键盘消失的唯一方法是在第一个文本字段中单击并单击“完成”或屏幕上的任何位置(不是文本字段)。

here is my code: 这是我的代码:

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

        }
    }
}

Here are some other methods used: 以下是一些其他使用的方法:

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

edit: 编辑:

More code: 更多代码:

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

This may not be the most efficient way to do this but im new to objective c and looking for a solution that I can understand. 这可能不是最有效的方法,但我是目标c的新手,并寻找一个我能理解的解决方案。 Thanks! 谢谢!

edit here is a picture 编辑这里是一张图片 键盘卡住了

please try to use this one 请尝试使用这个

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

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

and you can use second one way but this is not proper way...so i say to prefer first one method. 你可以使用第二种方式,但这不是正确的方式...所以我说首选一种方法。

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

for getting like this you can use this one 为了得到这样你可以使用这个

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

    return NO;
}

or 要么

otherwise better to use labels with tap Gestures then you can solve this problem easily. 否则最好使用带有水龙头手势的标签,那么你可以轻松解决这个问题。

Also you can try to use property of textField : inputView 您也可以尝试使用textField: inputView的属性

The custom input view to display when the text field becomes the first responder. 当文本字段成为第一个响应者时显示的自定义输入视图。

So, you can show this view instead of keyboard, and don't handle it manually. 因此,您可以显示此视图而不是键盘,并且不会手动处理它。

Or: 要么:

Wrong behavior for now occur, because keyboard for second textfield is shown also. 现在出现错误的行为,因为还显示了第二个文本字段的键盘。 So you need not only to resign first responder of first field, but also implement: 因此,您不仅需要辞退第一个字段的第一响应者,还需要实现:

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

UITextField includes the inputView and inputAccessoryView properties to address this very situation: UITextField包含inputViewinputAccessoryView属性来解决这种情况:

inputView

If the value in this property is nil , the text field displays the standard system keyboard when it becomes first responder. 如果此属性中的值为nil ,则文本字段在成为第一响应者时显示标准系统键盘。 Assigning a custom view to this property causes that view to be presented instead. 为此属性分配自定义视图会导致显示该视图。

if the view controller containing the textfield is presented as modalViewController with formsheet style , then you have to override the below given method to make it work properly. 如果包含文本域视图控制器是作为modalViewController港前赛绩的风格 ,那么你必须重写如下方法来使其正常工作。

- (BOOL)disablesAutomaticKeyboardDismissal 
{
   return NO;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM