iPhone-在UITextfield下显示错误消息的最佳方法是什么 [英] iPhone - What is the best way to display error message below UITextfield

查看:33
本文介绍了iPhone-在UITextfield下显示错误消息的最佳方法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含许多UITextfields的表单,例如Name,password,email等.在验证过程中,我想显示相应文本字段下方字段的错误消息.

I have a form with many UITextfields like Name, password, email, etc.., and during validation, i want to display the error message for the fields below the respective textfield.

执行此操作的最佳方法是什么.任何示例都将真正有帮助.

What is the best way to do this. Any examples would really help.

推荐答案

创建一个包含文本字段,标签和验证块的新对象模型.当文本字段退出第一响应者时,调用该块以检查其是否有效,如果无效,则显示标签

Create a new object model containing a textfield, a label and a validation block. When the textfield resigns first responder, call the block to check if it is valid and if not, display the label

类似的东西

@protocol ValidationTextFieldDelegate

-(void) validationTextField:(ValidationTextField*)textField didResignWithResult:(BOOL)result;

@end

@interface ValidationTextField : NSObject <UITextFieldDelegate>

@property (nonatomic, strong) UITextField *textField;
@property (nonatomic, strong) UILabel *errorLabel;
@property (copy) void(^validationBlock)(BOOL);
@property (nonatomic, assign) id<ValidationTextFieldDelegate> delegate;

@end

@implementation ValidationTextField

-(void)textFieldDidEndEditing:(UITextField*)textField {
    BOOL result = self.validationBlock();

    self.errorLabel.hidden = result;

    if (self.delegate) {
        [self.delegate validationTextField:self didResignWithResult:result];
    }
}

在您的控制器中

-(void) validationTextField:(ValidationTextField*)textField didResignWithResult:(BOOL)result {
    if (!result) {
        int yOffset = textField.errorLabel.bounds.size.height;

        // Update here the frames of your other textfields, adding the yOffset to their frames
    }
}

这篇关于iPhone-在UITextfield下显示错误消息的最佳方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆