1

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.

Harrison Xi
  • 766
  • 1
  • 5
  • 23
  • Try to comment the line [self.passwordTextField resignFirstResponder]; is the behavior the same ? – Rafouille Nov 17 '15 at 11:37
  • @Rafouille Yes. I added break point in the loginAction, and it is never triggered. – Harrison Xi Nov 19 '15 at 09:12
  • A blank project with two text fields, both linked to the same delegate, behaves correctly - return in the first field moves to the second, return in the second executes the login action. There's something else in your project that isn't in the question. Check that your outlets are connected properly and the delegates are set - are both outlets pointing to the same text field, that would probably cause it – jrturton Nov 19 '15 at 09:30
  • 1
    @jrturton Sorry for incomplete question. I have tested my codes. And I found that this issue only appears when I use RAC for these two textField. I will try to upload a simple project to Github and update this question. – Harrison Xi Nov 19 '15 at 10:22
  • I had the same issue and as commented on the answers below if you return NO on `textFieldShouldReturn` it works fine. Awkward to say the least. – Raphael Oliveira May 09 '16 at 06:48

2 Answers2

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;
}
shreyasm-dev
  • 2,711
  • 5
  • 16
  • 34
Rohit suvagiya
  • 1,005
  • 2
  • 12
  • 40
0
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
   NSUInteger index = textField.tag;
   if (index == 1 || textField == self.passwordTextField) 
   { 
      [textField resignFirstResponder];
   }
   else
   {
      [textField becomeFirstResponder];
   }
   return NO;
}
user3182143
  • 9,459
  • 3
  • 32
  • 39