Create Delegate

viewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UITextFieldDelegate>

@property UITextField *t1,*t2,*t3,*t4,*t5;


@end

viewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self UIElements];
    // Do any additional setup after loading the view, typically from a nib.
}
-(void)UIElements
{
    self.t1=[[UITextField alloc]initWithFrame:CGRectMake(20, 50, 100, 30)];
    self.t1.borderStyle=UITextBorderStyleBezel;
    self.t1.placeholder=@"name";
    self.t1.delegate=self;
    [self.view addSubview:self.t1];
    
    self.t2=[[UITextField alloc]initWithFrame:CGRectMake(20, 100, 100, 30)];
    self.t2.borderStyle=UITextBorderStyleBezel;
    self.t2.placeholder=@"name";
    self.t2.delegate=self;
    [self.view addSubview:self.t2];
    
    self.t3=[[UITextField alloc]initWithFrame:CGRectMake(20, 150, 100, 30)];
    self.t3.borderStyle=UITextBorderStyleBezel;
    self.t3.placeholder=@"name";
    self.t3.delegate=self;
    [self.view addSubview:self.t3];
    
    self.t4=[[UITextField alloc]initWithFrame:CGRectMake(20, 200, 100, 30)];
    self.t4.borderStyle=UITextBorderStyleBezel;
    self.t4.placeholder=@"name";
    self.t4.delegate=self;
    [self.view addSubview:self.t4];
    
    self.t5=[[UITextField alloc]initWithFrame:CGRectMake(20, 250, 100, 30)];
    self.t5.borderStyle=UITextBorderStyleBezel;
    self.t5.placeholder=@"name";
    self.t5.delegate=self;
    [self.view addSubview:self.t5];
}

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (textField==self.t1) {
        NSCharacterSet *t1Set=[NSCharacterSet characterSetWithCharactersInString:@"qwertyuioplkjhgfdsazxcvbnmMNBVCXZASDFGHJKLPOIUYTREWQ"];
        for (int i=0; i<[string length]; i++) {
            unichar c=[string characterAtIndex:i];
            if (![t1Set characterIsMember:c]) {
                return NO;
            }
        }
        NSUInteger t1Length=self.t1.text.length-range.length;
        return (t1Length<10);
    }
    
    return YES;
}

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    if ([textField isEqual:self.t2]) {
       self.t2.backgroundColor=[UIColor redColor];
        return YES;
    }
    
    return YES;
}

-(BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    if (textField==self.t3) {
        self.t4.backgroundColor=[UIColor yellowColor];
        return YES;
    }
    
    return YES;
}

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    if (textField==self.t4) {
        self.t5.hidden=YES;
    }
    
}
-(void)textFieldDidEndEditing:(UITextField *)textField
{
    if (textField==self.t3) {
        self.t4.hidden=YES;
    }
}

-(BOOL)textFieldShouldClear:(UITextField *)textField
{
    if (textField==self.t2) {
        self.t1.hidden=YES;
    }
    
    
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
Previous
Next Post »