【问题标题】:return being called twice in ios app返回在 ios 应用程序中被调用两次
【发布时间】:2014-10-20 13:09:03
【问题描述】:

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

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

我做错了什么?

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

【问题讨论】:

  • 你在- (void)textFieldDidBeginEditing:方法中写了什么吗?
  • 你还需要检查你的鼠标是否正常工作,我有同样类型的问题,我的鼠标单击在模拟器上双击。
  • 您可能应该返回 NO,除非您希望在登录名/密码中包含返回键字符。
  • @nazik 我没有实现这个方法。我也在物理设备上进行测试,而不是使用鼠标。
  • @CrimsonChris WOW 将其更改为返回 NO 成功了!

标签: ios objective-c login uitextfield becomefirstresponder


【解决方案1】:

试试这个:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-06
    • 1970-01-01
    相关资源
    最近更新 更多