代码之家  ›  专栏  ›  技术社区  ›  Neal L

使用多个UItextfields进行验证

  •  2
  • Neal L  · 技术社区  · 16 年前

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

    我读过 Text and Web Programming Guide 在苹果的支持文档中,它建议我将保存/验证逻辑放入 textFieldShouldEndEditing: :

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

    为了测试这个问题,我创建了一个简单的项目,其中有一个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;
    }
    

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

    1 回复  |  直到 11 年前
        1
  •  8
  •   Brennan    16 年前

    您可以在文本字段的右侧显示错误图标,而不是显示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,所以我将按钮设置为该大小。我确保按钮和图像适合文本字段,但是如果右视图太大,它会缩小。我把它设置到一个合适的大小,它不需要做任何缩放。

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

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