0

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

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

我究竟做错了什么?

登录视图控制器.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
4

1 回答 1

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;
    }

希望这可以帮助.. :)

于 2014-08-27T05:19:06.683 回答