使用多个 UITextField 进行验证

发布于 2024-09-07 19:44:52 字数 3499 浏览 2 评论 0原文

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

我已阅读文本编程指南 在 Apple 的支持文档中,建议我将保存/验证逻辑放在 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;
}

问题是:如果用户从一个 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 并正确显示错误?

I would like to create a UIView has multiple UITextFields validates each one as the user is done editing it. The view controller is the delegate for each of the UITextFields. When a user changes a value in one of those UITextFields and either touches "done" on the keyboard or touches on another one of the textfields in the view, I save and validate the change. The idea here is to give the user immediate feedback, and not allow him/her to proceed any further if there an invalid attribute value has been entered.

I have read the Text Programming Guide on Apple's support docs, which suggested that I put the save/validation logic in 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.

So to test this out, I created a simple project with one UIView and two UITextFields. Per the docs, all I do in this test project is display a UIAlertView and return NO. Here is the method:

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

And here's the problem: If the user clicks from one UITextField onto the other, this method is called 3 times and as a result the UIAlertView is displayed 3 times. Here is the console log from my testing:

-- 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!!!

So what's the deal? It seems that I am missing something... How do you validate a UITextField and display an error properly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乖乖兔^ω^ 2024-09-14 19:44:52

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

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

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

Instead of displaying a UIAlert you could instead show an error icon on the right side of the text field. This way if it does happen 3 times it does not cause the alert to come up and the user will not know that this icon is set 3 times. What I have done is set the right view with a button that has an image indicating the error. I associate the button with an action which shows an alert that tells the user what is wrong. I created a helper function which adds the image button to the right view with an action.

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

My ErrorIcon.png happens to be 15x28 so I make the button that size. I make sure the button and image fit the text field, although the right view will scale it down if it is too big. I set the it to a proper size does it does not have to do any scaling.

You can hide and show the right view using the mode that is shown in the above code. One mode is always and another is always. There are other modes that you may want to explore.

Now you can evaluate each field as a user finishes editing a text field and set the error as needed. I also have another set of matching validations which fire when the form is to be processed. I'd like to work it so that the same code is run for validation but I have not gone that far yet.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文