iphone - 使用多个 UITextField 进行验证

标签 iphone core-data uitextfield

我想创建一个具有多个 UITextField 的 UIView,在用户完成编辑后验证每个 UITextField。 View Controller 是每个 UITextField 的委托(delegate)。当用户更改这些 UITextField 之一中的值并触摸键盘上的“完成”或触摸 View 中的另一个文本字段时,我会保存并验证更改。这里的想法是给用户立即反馈,如果输入了无效的属性值,则不允许他/她继续进行。

我已阅读 Text Programming Guide在 Apple 的支持文档中,建议我将保存/验证逻辑放入 textFieldShouldEndEditing::

The best delegation methods for validating entered strings are textFieldShouldEndEditing: for text fields and textViewShouldEndEditing: for text views. These methods are called just before the text field or text view resigns first responder status. Returning NO prevents that from happening, and consequently the text object remains the focus of editing. If an entered string is invalid, you should also display an alert to inform the user of the error.

为了测试这一点,我创建了一个包含一个 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;
}

问题在于:如果用户从一个 UITextField 单击另一个 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!!!

所以这是怎么回事?看来我遗漏了一些东西...如何验证 UITextField 并正确显示错误?

最佳答案

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

- (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,所以我将按钮设置为这个大小。我确保按钮和图像适合文本字段,尽管如果太大,右 View 会将其缩小。我将其设置为适当的大小,它不需要进行任何缩放吗?

您可以使用上面代码中显示的模式隐藏和显示右侧 View 。一种模式是始终,另一种模式是始终。您可能还想探索其他模式。

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

关于iphone - 使用多个 UITextField 进行验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3166832/

相关文章:

ios - 苹果 map 使用 iOS App 设置目的地

ios - Xamarin iOS 8,UICollectionView - 出列可重用 UICollectionViewCell 时出错

ios - 查找周末发生的所有核心数据条目(使用 NSDate)

ios - 我应该多久删除一次并保存到核心数据

ios - 向 UITextField 添加前缀字符

iphone - 如果手机未连接到互联网,请加载不同的 Nib

iphone - XCode 在 "Run Script"阶段未完成测试构建

ios - 将 UIImage 保存到核心数据中; NSValueTransformers 的问题

ios - 在编辑时调整 UITextField 的大小

ios - 在 "self"中有多个项目?