第一响应者未正确设置 [英] first responder is not being set correctly

查看:99
本文介绍了第一响应者未正确设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个分组的表格视图,并尝试在用户点击输入时为文本字段设置新的第一响应者。

I have a grouped table view and trying to set a new first responder to a text field when the user hits enter.

这对我来说不是什么新鲜事,并且在我做了一些无关的更改之前,我的代码工作了,现在却没有。当我设置第一个响应者时,我有一个指向正确文本字段的指针,没有任何反应。

This is nothing new to me, and my code worked before I made some unrelated changes and now it doesn't. I have a pointer to the correct text field when I set its first responder, nothing happens.

焦点完全从任何文本字段消失,键盘停留在屏幕上。然后输入和隐藏键盘键都会停止工作,直到用户再次重新开始对文本字段的聚焦。

Focus goes away entirely from any text field and the keyboard stays on screen. Then both the enter and 'hide keyboard' keys stop functioning until the user re-initiates focus on a text field again.

以下是代码:

- (void)uiTextFieldShouldReturn:(ObjectEditTextField *)uiTextField
 {
        if ((group.fields.count - 1) > uiTextField.fieldTag) 
        {

       //loop through every table group
        for (int i = uiTextField.fieldTag + 1; i < group.fields.count; i++) {

            //get whatever field is in the row (not necessarily a text field)

            ObjectEditField *field = [group.fields objectAtIndex:i];

            // a check if the field is of type UITextField
            if (field.propName && field.updateObjectOnEdit == YES && [field isKindOfClass:[UITextField class]]) {
                // set the active field
                activeField = field;

                // adjust the table view offset to make sure the text field is visble
                [self setTableViewOffsetForTextField:field];

                // obtain a pointer to the textfield object and set it to be the first responder
                UITextField *textField = (UITextField *)field;
                NSLog(@"field %@", textField.fieldLabel);
                if (![textField.field isFirstResponder]) {

                    [textField.field becomeFirstResponder];
                    return; 
                }
                break;
            }
        }
    }


推荐答案

通过注释,该字段位于模态(呈现)控制器内。

By comments, the field is inside a modal (presented) controller.

首先解释 - 默认情况下,如果模态控制器中的文本字段失去焦点,键盘没有隐藏。这由方法控制 - [UIViewController disablesAutomaticKeyboardDismissal]

First an explanation - by default, if a text field in a modal controller loses focus, the keyboard is not hidden. This is controlled by method -[UIViewController disablesAutomaticKeyboardDismissal]


此默认实现当视图控制器的模式表示样式设置为UIModalPresentationFormSheet并且其他表示样式返回NO时,该方法返回YES。因此,系统通常不允许键盘被解散用于模态形式。

The default implementation of this method returns YES when the modal presentation style of the view controller is set to UIModalPresentationFormSheet and returns NO for other presentation styles. Thus, the system normally does not allow the keyboard to be dismissed for modal forms.

这解释了为什么键盘的行为方式如此。没有文本字段被聚焦,键盘不会被隐藏,因为我们在模态控制器内。

This explains why the keyboard behaves the way it behaves. No text field is focused and the keyboard is not hidden only because we are inside a modal controller.

那么,会发生什么?我们知道当前的文本字段会占用第一个响应者,并且没有视图成为第一响应者或者不可编辑的视图成为第一响应者。

So, what happens? We know that the current text field resigns first responder and either no view becomes first responder or a non-editable view becomes first responder.

嗯,您的整个代码看起来非常可疑:

Well, you whole code looks very suspicious:

ObjectEditField *field = [group.fields objectAtIndex:i];

这里我们有一个 ObjectEditField 类型的对象

Here we have an object of type ObjectEditField

[field isKindOfClass:[UITextField class]]

现在,我们正在测试,如果该字段是 UITextField 的子类。那非常可疑。如果它是 ObjectEditField ,它怎么可能 UITextField 呢?

Now, we are testing, if the field is a subclass of UITextField. That's very suspicious. If it's ObjectEditField, how it can be UITextField, too?

让我们继续

UITextField *textField = (UITextField *)field;

Okey,我们假设它是 UITextField

Okey, let's assume it's a UITextField

if (![textField.field isFirstResponder]) {

同样,非常可疑的东西。 UITextField 没有字段属性。

Again, something very suspicious. A UITextField has no field property.

所以:

[textField.field becomeFirstResponder];

在未知对象上调用。我们甚至不知道它是否是 UITextField

is called on an unknown object. We don't even know if it's a UITextField.

请注意,大多数代码都应在您的警告中发出警告IDE。

Note that most of the code should issue warnings in your IDE.

解决方案:


  1. 清理代码,检查警告。

  2. becomeFirstResponder NSLog(@%@,textField.field) $ c>致电。检查日志。

  1. Clean up your code, check warnings.
  2. Put a NSLog(@"%@", textField.field) before the becomeFirstResponder call. Check the log.

这篇关于第一响应者未正确设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆