UITextField 获得焦点,然后由于在 textFieldShouldReturn 中返回 YES 而立即失去焦点 [英] UITextField get focus and then lose focus immediately due to return YES in textFieldShouldReturn
问题描述
这是我的代码:
- (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.有人可以帮我吗?
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?
========
添加更多信息:
我发现这个问题只有在我使用 ReactiveCocoa 时才会出现.ReactiveCocoa 的版本是 2.5.
I found that this issue will only appear when I user ReactiveCocoa. The version of ReactiveCocoa is 2.5.
推荐答案
只需设置标签您的文本字段并输入此代码
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;
}
这篇关于UITextField 获得焦点,然后由于在 textFieldShouldReturn 中返回 YES 而立即失去焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!