我正在为我的应用创建一个登录信息,我遇到了UITextField's
响应者的问题。
我有两个字段,其中一个用于电子邮件,另一个用于密码。第一个,返回的是“下一个”按钮,第二个是“完成”按钮。
问题是我正在为那些按钮开发逻辑。 textfieldShouldReturn
方法是这样的:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if ([emailField isFirstResponder]) {
NSLog(@"NEXT");
} else if ([passwordField isFirstResponder]) {
[passwordField resignFirstResponder];
}
return YES;
}
关于字段的代码:
CGRect frame = CGRectMake(20, 0, 280, 44);
emailField = [[UITextField alloc] initWithFrame:frame];
[emailField setPlaceholder:@"Correo electronico"];
[emailField setKeyboardType:UIKeyboardTypeEmailAddress];
[emailField setReturnKeyType:UIReturnKeyNext];
[emailField setClearButtonMode:UITextFieldViewModeWhileEditing];
[emailField setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
[emailField setAutocapitalizationType:UITextAutocapitalizationTypeNone];
[emailField setAutocorrectionType:UITextAutocorrectionTypeNo];
[emailField setDelegate:self];
passwordField = [[UITextField alloc] initWithFrame:frame];
[passwordField setPlaceholder:@"Contraseña"];
[passwordField setKeyboardType:UIKeyboardTypeDefault];
[passwordField setReturnKeyType:UIReturnKeyDone];
[passwordField setSecureTextEntry:YES];
[passwordField setClearButtonMode:UITextFieldViewModeWhileEditing];
[passwordField setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
[passwordField setAutocapitalizationType:UITextAutocapitalizationTypeNone];
[passwordField setAutocorrectionType:UITextAutocorrectionTypeNo];
[passwordField setDelegate:self];
当我在emailField
并按下接下来的按键时,没有任何反应,但是如果我在passwordField
并按下完成键,该应用程序正常工作并隐藏键盘。
答案 0 :(得分:0)
这是我的代码
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if (textField == fieldUsername) {
[fieldPassword becomeFirstResponder];
} else if (textField == fieldPassword) {
[self doLogin];
}
return YES;
}
答案 1 :(得分:0)
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if (textField == emailField) {
NSLog(@"NEXT");
[emailField resignFirstResponder];
[passwordField becomeFirstResponder];
}
else if (textField == passwordField) {
[passwordField resignFirstResponder];
}
return YES;
}
答案 2 :(得分:0)
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if ([emailField isFirstResponder]) {
NSLog(@"NEXT");
[passwordField becomefirstresponder];///add this
} else if ([passwordField isFirstResponder]) {
[passwordField resignFirstResponder];
}
return YES;
}