返回在ios应用程序中被调用两次

时间:2014-08-27 04:18:22

标签: ios objective-c login uitextfield becomefirstresponder

我正在尝试使用用户名和密码文本字段在我的iOS应用中实现一个非常基本的登录屏幕。

在用户点击“下一步”输入用户名后,我查看了如何让视图自动关注密码文本字段。但是,出于某种原因,在用户输入用户名后,似乎检测到两次返回。键盘不会将键盘保持在屏幕上并专注于密码textField,而是键盘消失,而且textField都没有聚焦。

我做错了什么?

LoginViewController.m:

@implementation LoginViewController


    - (void)viewDidLoad
    {
        [super viewDidLoad];
        [self.navigationController setNavigationBarHidden:YES];
        username = [[NSString alloc]init];
        keychainItem = [[KeychainItemWrapper alloc]initWithIdentifier:@"Login" accessGroup:nil];    
    }

    // Check if user presses next from the username textfield -> automatically go to password
    - (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
        if (theTextField == self.txtPassword) {
            // try logging in
            NSLog(@"trying to proceed to submit form");

        } else if (theTextField == self.txtUsername) {
            // automatically go to the password field
            NSLog(@"trying to proceed to password field");
            [self.txtPassword becomeFirstResponder];
        }
        return YES;
    }

当我查看我的日志时,用户点击用户名字段中的“下一步”后会出现以下两个日志

2014-08-27 00:11:48.594 BiP[3049:60b] trying to proceed to password field
2014-08-27 00:11:48.640 BiP[3049:60b] trying to proceed to submit form

1 个答案:

答案 0 :(得分:0)

试试这个:

- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
        if (theTextField == self.txtPassword) {
            // try logging in
            NSLog(@"trying to proceed to submit form");
            return YES;

        } else if (theTextField == self.txtUsername) {
            // automatically go to the password field
            NSLog(@"trying to proceed to password field");
            [self.txtPassword becomeFirstResponder];
            return NO;
        }
        return YES;
    }

希望这会有所帮助.. :)