问题
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