使用多个UITextField进行验证

Nea*_*l L 2 iphone core-data uitextfield

我想创建一个UIView有多个UITextFields验证每个UITextFields用户完成编辑它.视图控制器是每个UITextField的委托.当用户更改其中一个UITextFields中的值并触摸键盘上的"done"或触摸视图中的另一个文本字段时,我保存并验证更改.这里的想法是给用户立即反馈,如果输入了无效的属性值,则不允许他/她继续进行.

我已经阅读了Apple支持文档上的Text and Web Programming Guide,它建议我将保存/验证逻辑放在textFieldShouldEndEditing::

验证输入字符串的最佳委托方法是textFieldShouldEndEditing:用于文本字段,textViewShouldEndEditing:用于文本视图.在文本字段或文本视图重新调出第一响应者状态之前调用这些方法.返回NO可防止发生这种情况,因此文本对象仍然是编辑的焦点.如果输入的字符串无效,您还应显示警告以通知用户该错误.

所以为了测试这个,我用一个UIView和两个UITextField创建了一个简单的项目.根据文档,我在这个测试项目中所做的只是显示一个UIAlertView并返回NO.这是方法:

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    // return YES to allow editing to stop and to resign first responder status. NO to disallow the editing session to end
    NSLog(@"In function: textFieldShouldEndEditing:(UITextField *)textField (tag=%i)", textField.tag);
    [self logFirstResponder];

    // PRETEND THAT THERE IS AN ISSUE THAT FAILS VALIDATION AND DISPLAY
    // A UIALERTVIEW.
    UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Uh Oh!",@"")
                                                         message:@"This is a test error"
                                                        delegate:self
                                               cancelButtonTitle:NSLocalizedString(@"OK",@"")
                                               otherButtonTitles:nil];
    [errorAlert show];
    [errorAlert release];
    NSLog(@"Displaying Error UIAlertView!!!");

    // SINCE THE VALIDATION FAILED, RETURN NO TO HOLD THE USER IN THE
    // UITEXTFIELD.
    return NO;
}
Run Code Online (Sandbox Code Playgroud)

这就是问题所在:如果用户从一个UITextField点击另一个,则会调用此方法3次,因此UIAlertView会显示3次.这是我测试的控制台日志:

-- Field One tag = 100, Field Two tag = 200 --

2010-07-02 09:52:57.971 test project[22866:207] In function: textFieldShouldBeginEditing:(UITextField *)textField (tag=100)
2010-07-02 09:52:57.977 test project[22866:207] In function: textFieldDidBeginEditing:(UITextField *)textField (tag=100)
2010-07-02 09:52:57.977 test project[22866:207] Field One is the First Responder.

-- now i'm going to click from Field One into Field Two --

2010-07-02 09:53:18.771 test project[22866:207] In function: textFieldShouldBeginEditing:(UITextField *)textField (tag=200)
2010-07-02 09:53:18.772 test project[22866:207] Field One is the First Responder.
2010-07-02 09:53:18.774 test project[22866:207] In function: textFieldShouldEndEditing:(UITextField *)textField (tag=100)
2010-07-02 09:53:18.774 test project[22866:207] Field One is the First Responder.
2010-07-02 09:53:18.778 test project[22866:207] Displaying Error UIAlertView!!!
2010-07-02 09:53:18.780 test project[22866:207] In function: textFieldShouldBeginEditing:(UITextField *)textField (tag=200)
2010-07-02 09:53:18.781 test project[22866:207] Field One is the First Responder.
2010-07-02 09:53:18.781 test project[22866:207] In function: textFieldShouldEndEditing:(UITextField *)textField (tag=100)
2010-07-02 09:53:18.782 test project[22866:207] Field One is the First Responder.
2010-07-02 09:53:18.783 test project[22866:207] Displaying Error UIAlertView!!!
Run Code Online (Sandbox Code Playgroud)

那是什么交易?似乎我遗漏了一些东西......你如何验证UITextField并正确显示错误?

Bre*_*nan 8

您可以在文本字段的右侧显示错误图标,而不是显示UIAlert.这样,如果它确实发生了3次,它不会导致警报出现,并且用户将不知道该图标被设置3次.我所做的是使用一个按钮设置右视图,该按钮具有指示错误的图像.我将该按钮与一个动作相关联,该动作显示一个警告,告诉用户出了什么问题.我创建了一个辅助函数,通过动作将图像按钮添加到右视图中.

- (void)setErrorForTextField:(UITextField *)textField target:(id)target action:(SEL)selector
{
    CGRect frame = CGRectMake(0.0, 0.0, 15.0, 28.0);
    UIImage *image = [UIImage imageNamed:@"ErrorIcon.png"];
    UIButton *button = [[[UIButton alloc] initWithFrame:frame] autorelease];
    [button setImage:image forState:UIControlStateNormal];
    button.backgroundColor = [UIColor clearColor];

    [button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
    textField.rightView = button;
    textField.rightViewMode = UITextFieldViewModeAlways;
}
Run Code Online (Sandbox Code Playgroud)

我的ErrorIcon.png恰好是15x28,所以我按下那个大小的按钮.我确保按钮和图像适合文本字段,但如果它太大,右视图会缩小它.我将它设置为适当的大小,它不必进行任何缩放.

您可以使用上面代码中显示的模式隐藏和显示正确的视图.一种模式始终是另一种模式.您可能想要探索其他模式.

现在,您可以在用户完成编辑文本字段并根据需要设置错误时评估每个字段.我还有另一组匹配的验证,在处理表单时触发.我想工作它,以便运行相同的代码进行验证,但我还没有走得那么远.