【问题标题】:Issues dismissing keyboard conditionally有条件地关闭键盘的问题
【发布时间】:2010-03-30 16:20:35
【问题描述】:

我有一个包含用户名和密码字段的应用程序。我想在允许用户停止编辑字段之前验证输入。为此,我使用了 textFieldShouldEndEditing 委托方法。如果输入未验证,我会显示 UIAlertView。

这种方法如宣传的那样工作 - 如果输入未通过验证,用户将无法离开该字段。

要让键盘上的完成按钮关闭键盘,我在文本字段上调用 ​​resignFirstResponder。

我遇到的问题是警报被调用了两次。如何让提醒不显示两次?

编辑澄清

发生的情况是出现警报,然后出现另一个警报。然后我必须关闭两个窗口来修复输入。

这里是 textFieldShouldEndEditing 方法

-(BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    NSLog(@"function called %@",textField);
    if([textField.text length] == 0)
    {
        return YES;
    }
    if(textField == userName)
    {
    if([self userNameValidated:textField.text])
    {
        NSLog(@"name validated");
        NSString *tempDonerName = [[NSString alloc] initWithString:(@"%@",userName.text)];
        //[[NSUserDefaults standardUserDefaults] setObject:tempDonerName forKey:@"name"];
        [tempDonerName release];
        return YES;
    } else {
        NSLog(@"name did not validate");
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Invalid Username",@"Invalid Username title")
                                                        message:NSLocalizedString(@"Please make sure there are no apostrophes,spaces in the username, and that the username is less than 12 characters",@"Invalid username message")
                                                       delegate:nil
                                              cancelButtonTitle:NSLocalizedString(@"OK",@"OK Text") 
                                              otherButtonTitles:nil];
        [alert show];
        [alert release];            

        return NO;
    }

} else if (textField == userPin) {
    if([self userPinValidated:textField.text])
    {
        NSLog(@"pin validated");
        //NSString *tempDonerPin = [[NSString alloc] initWithString:(@"%@",userPin.text)];              
        //[[NSUserDefaults standardUserDefaults] setObject:tempDonerPin forKey:@"pin"];
        //[tempDonerPin release];
        return YES;
    } else {
        NSLog(@"pin did not validate");
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Invalid Password",@"Invalid Pin title")
                                                        message:NSLocalizedString(@"Please make sure there are no apostrophes in the password",@"Invalid password message")
                                                       delegate:nil
                                              cancelButtonTitle:NSLocalizedString(@"OK",@"OK Text") 
                                              otherButtonTitles:nil];
        [alert show];
        [alert release];

        return NO;
    }
}else {
    NSLog(@"code validate - shouldn't get called");
    return YES;
}

}

【问题讨论】:

  • 你能发布你的代码吗?
  • 您的意思是用户按下完成按钮一次后,警报显示两次?或者用户必须在验证名称之前按两次(或三次?)完成按钮?看起来你处理字符串有点奇怪,所以它可能与此有关。或者问题可能出在您的验证方法中。很难从中分辨出来。
  • 这不会帮助您解决问题,但您可以通过使用 UITextField 的“标签”字段来指定哪个字段,使您的代码更加模块化 - 并且可读/自记录是哪个,然后对每个可能的标签使用 switch 语句。

标签: iphone objective-c cocoa-touch


【解决方案1】:

抛开双重警告:resignFirstResponder 将调用 textFieldShouldEndEditing,这是没有办法的。因此,您需要传递一个标志,表明使用未经验证的输入退出该字段很酷,或者清除内容;或由用户决定并启用字段上的清除按钮。

【讨论】:

  • 我害怕那个。我要尝试的是仅在当前不可见时才显示警报。
猜你喜欢
  • 2011-07-21
  • 1970-01-01
  • 2012-10-18
  • 1970-01-01
  • 2011-09-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-20
相关资源
最近更新 更多