以下是我的代码:
- (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中单击return键时将焦点移到passwordTextField。但是passwordTextField获得焦点然后立即失去焦点。我创建了UITextField
的子类并尝试找到原因。我发现如果我在函数textFieldShouldReturn
中返回YES,则passwordTextField将获得insertText
调用。然后passwordTextField将立即收到resignFirstResponder
电话。我现在还不知道为什么我们必须在函数textFieldShouldReturn
中返回NO 。任何人都可以帮助我吗?
======
添加更多信息:
我发现只有在我使用ReactiveCocoa时才会出现此问题。 ReactiveCocoa的版本是2.5。
答案 0 :(得分: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;
}
我希望这会对你有所帮助。
如果您喜欢我的答案,请接受并提出我的答案
答案 1 :(得分:0)
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
NSUInteger index = textField.tag;
if (index == 1 || textField == self.passwordTextField)
{
[textField resignFirstResponder];
}
else
{
[textField becomeFirstResponder];
}
return NO;
}