【问题标题】:UITextField get focus and then lose focus immediately due to return YES in textFieldShouldReturn由于在 textFieldShouldReturn 中返回 YES,UITextField 获得焦点然后立即失去焦点
【发布时间】:2015-11-17 11:32:27
【问题描述】:

这是我的代码:

- (void)viewDidLoad {
    [super viewDidLoad];

    // passwordTextField cannot get focus after click next key due to this RAC
    RAC(self.loginButton, enabled) = [RACSignal combineLatest:@[self.userTextField.rac_textSignal,
                                                                self.passwordTextField.rac_textSignal]
                                                       reduce:^id (NSString *user, NSString *password) {
                                                           if ([user length] > 0 && [password length] > 0) {
                                                               return @YES;
                                                           }
                                                           return @NO;
                                                       }];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (textField == self.userTextField) {
        [self.passwordTextField becomeFirstResponder];
    } else {
        [self loginAction:textField];
    }
    // passwordTextField cannot get focus after click next key due to returning YES
    return YES;
}

- (void)loginAction:(id)sender
{
    [self.userTextField resignFirstResponder];
    [self.passwordTextField resignFirstResponder];
    // some login actions
}

当单击 userTextField 中的返回键时,我想将焦点移至 passwordTextField。但是 passwordTextField 获得焦点然后立即失去焦点。我创建了UITextField 的子类并试图找出原因。我发现如果我在函数textFieldShouldReturn 中返回YES,passwordTextField 将得到insertText 调用。然后passwordTextField 将立即收到resignFirstResponder 调用。我现在不知道为什么我们必须在函数textFieldShouldReturn 中返回NO。谁能帮帮我?

========

添加更多信息:

我发现这个问题只有在我使用 ReactiveCocoa 时才会出现。 ReactiveCocoa 的版本是 2.5。

【问题讨论】:

  • 尝试注释行 [self.passwordTextField resignFirstResponder];行为是否相同?
  • @Rafouille 是的。我在 loginAction 中添加了断点,它从未被触发。
  • 一个带有两个文本字段的空白项目,都链接到同一个委托,行为正确 - 第一个字段中的返回移动到第二个字段,第二个字段中的返回执行登录操作。您的项目中还有其他问题不在问题范围内。检查您的插座是否正确连接并且代表已设置 - 两个插座都指向相同的文本字段,这可能会导致它
  • @jrturton 抱歉,问题不完整。我已经测试了我的代码。而且我发现只有当我对这两个文本字段使用 RAC 时才会出现这个问题。我会尝试将一个简单的项目上传到 Github 并更新这个问题。
  • 我遇到了同样的问题,如果您在textFieldShouldReturn 上返回“否”,则在下面的答案中评论它可以正常工作。至少可以说很尴尬。

标签: ios objective-c uitextfield reactive-cocoa


【解决方案1】:

只需设置标签您的文本字段并输入此代码

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    NSInteger nextTag = textField.tag + 1;
    // Try to find next responder
    UIResponder* nextResponder = [textField.superview viewWithTag:nextTag];
    if (textField.tag == 0) {
        // Found next responder, so set it.
        [nextResponder becomeFirstResponder];
    } else {
        // Not found, so remove keyboard.
        [textField resignFirstResponder];
    }
    return NO;
}

【讨论】:

    【解决方案2】:
    - (BOOL)textFieldShouldReturn:(UITextField *)textField
    {
       NSUInteger index = textField.tag;
       if (index == 1 || textField == self.passwordTextField) 
       { 
          [textField resignFirstResponder];
       }
       else
       {
          [textField becomeFirstResponder];
       }
       return NO;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-23
      • 2011-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多