Iphone 使用多个UITextField进行验证

Iphone 使用多个UITextField进行验证,iphone,core-data,uitextfield,Iphone,Core Data,Uitextfield,我想创建一个UIView,它有多个UIExtFields,在用户完成编辑时验证每个UIExtFields。视图控制器是每个UITextFields的委托。当用户更改其中一个UITextFields中的值并触摸键盘上的“完成”或触摸视图中的另一个textfields时,我保存并验证更改。这里的想法是立即向用户提供反馈,如果输入了无效的属性值,则不允许他/她继续操作 我已经阅读了苹果的支持文档,其中建议我将保存/验证逻辑放入文本字段shouldendediting:: 验证输入字符串的最佳委托方法是

我想创建一个UIView,它有多个UIExtFields,在用户完成编辑时验证每个UIExtFields。视图控制器是每个UITextFields的委托。当用户更改其中一个UITextFields中的值并触摸键盘上的“完成”或触摸视图中的另一个textfields时,我保存并验证更改。这里的想法是立即向用户提供反馈,如果输入了无效的属性值,则不允许他/她继续操作

我已经阅读了苹果的支持文档,其中建议我将保存/验证逻辑放入
文本字段shouldendediting:

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

为了验证这一点,我创建了一个简单的项目,其中包含一个UIView和两个UITextFields。根据文档,我在这个测试项目中所做的就是显示UIAlertView和返回号。方法如下:

- (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;
}
问题是:如果用户从一个UIExtField点击另一个UIExtField,此方法被调用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!!!

那怎么办?似乎我错过了什么。。。如何验证UITextField并正确显示错误?

您可以在文本字段的右侧显示错误图标,而不是显示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;
}
我的ErrorIcon.png恰好是15x28,所以我将按钮设置为该大小。我确保按钮和图像适合文本字段,尽管如果它太大,右视图会缩小它。我将它设置为适当的大小,它不必进行任何缩放

您可以使用上述代码中显示的模式隐藏和显示右视图。一种模式总是,另一种模式总是。您可能还需要探索其他模式

现在,您可以在用户完成编辑文本字段并根据需要设置错误时计算每个字段。我还有另一组匹配的验证,在处理表单时触发。我希望使用它,以便运行相同的代码进行验证,但我还没有做到这一点