I want to validate UITextField input for multiple view controllers. The following works:
validate.h
#import <UIKit/UIKit.h>
@interface validate : UITextField <UITextFieldDelegate>
@end
validate.m
#import "validate.h"
@implementation validate
viewController.h
#import <UIKit/UIKit.h>
#include "limiteTextField.h"
@interface ViewController : UIViewController <UITextFieldDelegate>
@property (strong, nonatomic) IBOutlet limiteTextField *myTextField;
@end
viewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
//my code for validating
}
- (void)viewDidLoad {
[super viewDidLoad];
_myTextField.delegate=self;
I want to be able to use the shouldChangeCharactersInRange as an external function so don't have to rewrite all its code for each view controller.
One approach is to create some kind of validator class with a singleton and assigning it as the textfields delegate:
TextFieldValidator.h
TextFieldValidator.m
SomeViewController.m