Create a new object model containing a text box, label, and validation block. When the text field resigns the first responder, call the block to check if it is valid, and if not, display the label
Something like that
@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];
}
}
And in your controller
-(void) validationTextField:(ValidationTextField*)textField didResignWithResult:(BOOL)result {
if (!result) {
int yOffset = textField.errorLabel.bounds.size.height;
}
}
source
share