UITextField get focus and then lose focus immediately due to return YES in textFieldShouldReturn

只谈情不闲聊 提交于 2019-12-08 03:32:54

问题


Here are my codes:

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

I want to move focus to passwordTextField when click return key in userTextField. But the passwordTextField get focus and then lose focus immediately. I created a subclass of UITextField and try to find the reason. I found that if I return YES in function textFieldShouldReturn, the passwordTextField will get a insertText call. Then the passwordTextField will receive resignFirstResponder call immediately. I don't know why we must return NO in function textFieldShouldReturn now. Any one can help me?

========

Add more infomation:

I found that this issue will only appear when I user ReactiveCocoa. The version of ReactiveCocoa is 2.5.


回答1:


Just set tag your textfield and put this code

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

I hope this will help you great.

If you like my Answer so accept and upvote my answer




回答2:


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


来源:https://stackoverflow.com/questions/33755870/uitextfield-get-focus-and-then-lose-focus-immediately-due-to-return-yes-in-textf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!