Alert view Controller

viewcontroller.h
#import <UIKit/UIKit.h>


@interface ViewController : UIViewController<UITextFieldDelegate>
@property UILabel *firstName,*lastName,*mobile,*gender,*city,*address,*email,*password,*rePassword;
@property UITextField *firstText,*lastText,*mobileText,*cityText,*emailText,*passwordText,*rePasswordText;
@property UISegmentedControl *userSeg,*genSeg,*agreeSeg;
@property UITextView *addressView;
@property UIButton *submit;
@property UIScrollView *exitView,*userView,*loginView;
//login
@property UILabel *userName,*userPassword;
@property UITextField *userNameText,*userPasswordText;
@property UIButton *login;
@property NSMutableArray *ary;
@property NSMutableDictionary *dic,*savingData;
@property UILabel *firstN,*lastN,*mobileN,*genderN,*cityN,*addressN,*emailN,*passwordN;

@end

viewcontroller.m
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self UIElements];
    self.view.backgroundColor=[UIColor grayColor];
    self.ary=[[NSMutableArray alloc]init];
    self.dic =[[NSMutableDictionary alloc]init];
    self.savingData=[[NSMutableDictionary alloc]init];
}

-(void)UIElements
{
    self.userSeg=[[UISegmentedControl alloc]initWithItems:@[@"NewUser",@"ExistingUser"]];
    self.userSeg.frame=CGRectMake(30, 50, 200, 30);
    self.userSeg.backgroundColor=[UIColor orangeColor];
    [self.userSeg addTarget:self action:@selector(showData) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:self.userSeg];
    
    self.userView=[[UIScrollView alloc]initWithFrame:CGRectMake(0,80, 350, 700)];
     self.userView.contentSize=CGSizeMake(1000, 1000);
    [self.view addSubview:self.userView];
    
    self.exitView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 80, 350, 700)];
    [self.view addSubview:self.exitView];
    
    self.loginView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 80, 350, 700)];
    self.loginView.contentSize=CGSizeMake(1000, 1000);
    [self.view addSubview:self.loginView];
    
    //new User
    
    //first name
    self.firstName=[[UILabel alloc]initWithFrame:CGRectMake(20, 0, 100, 30)];
    self.firstName.text=@"First Name";
    [self.userView addSubview:self.firstName];
    
    self.firstText=[[UITextField alloc]initWithFrame:CGRectMake(150, 0, 150, 30)];
    self.firstText.placeholder=@"First Name";
    self.firstText.clearButtonMode=YES;
    self.firstText.clearsOnBeginEditing=YES;
    self.firstText.delegate=self;
    [self.userView addSubview:self.firstText];
    
    //lastname
    self.lastName=[[UILabel alloc]initWithFrame:CGRectMake(20, 50, 100, 30)];
    self.lastName.text=@"Last Name";
    [self.userView addSubview:self.lastName];
    
    self.lastText=[[UITextField alloc]initWithFrame:CGRectMake(150, 50, 150, 30)];
    self.lastText.placeholder=@"Last Name";
    self.lastText.clearButtonMode=YES;
    self.lastText.clearsOnBeginEditing=YES;
     self.lastText.delegate=self;
    [self.userView addSubview:self.lastText];
    
    //gender
    self.gender=[[UILabel alloc]initWithFrame:CGRectMake(20, 100, 100, 30)];
    self.gender.text=@"Gender";
    [self.userView addSubview:self.gender];
    
    self.genSeg=[[UISegmentedControl alloc]initWithItems:@[@"Male",@"Female"]];
    self.genSeg.frame=CGRectMake(150, 100, 150, 30);
    [self.userView addSubview:self.genSeg];
    
    //mobile
    self.mobile=[[UILabel alloc]initWithFrame:CGRectMake(20, 150, 100, 30)];
    self.mobile.text=@"Mobile";
    [self.userView addSubview:self.mobile];
    
    self.mobileText=[[UITextField alloc]initWithFrame:CGRectMake(150, 150, 150, 30)];
    self.mobileText.placeholder=@"Mobile";
    self.mobileText.delegate=self;
    self.mobileText.clearsOnBeginEditing=YES;
    self.mobileText.clearButtonMode=YES;
    [self.userView addSubview:self.mobileText];
    
    //city
    self.city=[[UILabel alloc]initWithFrame:CGRectMake(20, 200, 100, 30)];
    self.city.text=@"City";
    [self.userView addSubview:self.city];
    
    self.cityText=[[UITextField alloc]initWithFrame:CGRectMake(150, 200, 150, 30)];
    self.cityText.placeholder=@"City";
    self.cityText.delegate=self;
    self.cityText.clearButtonMode=YES;
    self.cityText.clearsOnBeginEditing=YES;
    [self.userView addSubview:self.cityText];
    
    //email
    self.email=[[UILabel alloc]initWithFrame:CGRectMake(20, 250, 100, 30)];
    self.email.text=@"Email";
    [self.userView addSubview:self.email];
    
    self.emailText=[[UITextField alloc]initWithFrame:CGRectMake(150, 250, 150, 30)];
    self.emailText.placeholder=@"Email";
    self.emailText.delegate=self;
    self.emailText.clearsOnBeginEditing=YES;
    self.emailText.clearButtonMode=YES;
    [self.userView addSubview:self.emailText];
    
    //password
    self.password=[[UILabel alloc]initWithFrame:CGRectMake(20, 300, 100, 30)];
    self.password.text=@"Password";
    [self.userView addSubview:self.password];
    
    self.passwordText=[[UITextField alloc]initWithFrame:CGRectMake(150, 300, 150, 30)];
    self.passwordText.placeholder=@"password";
    self.passwordText.delegate=self;
    self.passwordText.clearButtonMode=YES;
    self.passwordText.clearsOnBeginEditing=YES;
    [self.userView addSubview:self.passwordText];
    
    //repassword
    self.rePassword=[[UILabel alloc]initWithFrame:CGRectMake(20, 350, 100, 30)];
    self.rePassword.text=@"RePassword";
    [self.userView addSubview:self.rePassword];
    
    self.rePasswordText=[[UITextField alloc]initWithFrame:CGRectMake(150, 350, 150, 30)];
    self.rePasswordText.placeholder=@"Repassword";
    self.rePasswordText.delegate=self;
    self.rePasswordText.clearsOnBeginEditing=YES;
    self.rePasswordText.clearButtonMode=YES;
    [self.userView addSubview:self.rePasswordText];
    
    //address
    self.address=[[UILabel alloc]initWithFrame:CGRectMake(20, 400, 100, 30)];
    self.address.text=@"Address";
    [self.userView addSubview:self.address];
    
    self.addressView=[[UITextView alloc]initWithFrame:CGRectMake(150, 400, 150, 50)];
    [self.userView addSubview:self.addressView];
    
    
    //agree
    self.agreeSeg=[[UISegmentedControl alloc]initWithItems:@[@"Agree",@"DisAgree"]];
    self.agreeSeg.frame=CGRectMake(100, 500, 150, 30) ;
    [self.agreeSeg addTarget:self action:@selector(agree) forControlEvents:UIControlEventValueChanged];
    [self.userView addSubview:self.agreeSeg];
    
}
-(void)showData
{
    if (self.userSeg.selectedSegmentIndex==0) {
        self.userView.hidden=NO;
        self.exitView.hidden=YES;
        self.loginView.hidden=YES;
    }
    else if(self.userSeg.selectedSegmentIndex==1)
    {
        self.userView.hidden=YES;
        self.exitView.hidden=NO;
        self.loginView.hidden=YES;
        
        //existinguser
        //username
        self.userName=[[UILabel alloc]initWithFrame:CGRectMake(20, 0, 100, 30)];
        self.userName.text=@"UserName";
        [self.exitView addSubview:self.userName];
        
        self.userNameText=[[UITextField alloc]initWithFrame:CGRectMake(150, 0, 150, 30)];
        self.userNameText.placeholder=@"UserName";
        [self.exitView addSubview:self.userNameText];
        
        //userpassword
        self.userPassword=[[UILabel alloc]initWithFrame:CGRectMake(20, 50, 100, 30)];
        self.userPassword.text=@"Password";
        [self.exitView addSubview:self.userPassword];
        
        self.userPasswordText=[[UITextField alloc]initWithFrame:CGRectMake(150, 50, 150, 30)];
        self.userPasswordText.placeholder=@"Password";
        [self.exitView addSubview:self.userPasswordText];
        
        //login
        self.login=[UIButton buttonWithType:UIButtonTypeSystem];
        self.login.frame=CGRectMake(100, 100, 100, 30);
        [self.login setTitle:@"Login" forState:UIControlStateNormal];
        [self.login addTarget:self action:@selector(details) forControlEvents:UIControlEventTouchUpInside];
        [self.exitView addSubview:self.login];
        
        
        UIAlertController *alert=[UIAlertController alertControllerWithTitle:@"User Details" message:@"Users" preferredStyle:UIAlertControllerStyleActionSheet];
        for (int i=0; i<self.savingData.count; i++) {
            NSString *firstName=self.savingData.allKeys[i];
            UIAlertAction * action=[UIAlertAction actionWithTitle:firstName style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                self.userNameText.text=[self.savingData valueForKey:firstName];
                
        
            }];
            [alert addAction:action];
        }
            UIAlertAction *actionCancel=[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
                
            }];
             [alert addAction:actionCancel];
            
        
        [self presentViewController:alert animated:YES completion:nil];
    }
    
}
-(void)details
{
    self.userView.hidden=YES;
    self.exitView.hidden=YES;
    self.loginView.hidden=NO;
    for (int i=0; i<=self.ary.count-1; i++) {
        NSMutableDictionary *pers=[self.ary objectAtIndex:i];
        NSString *firstName=[pers objectForKey:@"FN"];
        NSString *lastName=[pers objectForKey:@"LN"];
        NSString *genderName=[pers objectForKey:@"GN"];
        NSString *mobile=[pers objectForKey:@"MN"];
        NSString *city=[pers objectForKey:@"CN"];
        NSString *email=[pers objectForKey:@"EN"];
        NSString *password=[pers objectForKey:@"PN"];
        
        if ([email isEqualToString:self.userNameText.text] && [password isEqualToString:self.userPasswordText.text]) {
            //first name
            self.firstName=[[UILabel alloc]initWithFrame:CGRectMake(20, 0, 100, 30)];
            self.firstName.text=@"First Name";
            [self.loginView addSubview:self.firstName];
            
            self.firstN=[[UILabel alloc]initWithFrame:CGRectMake(150, 0, 150, 30)];
            [self.firstN setText:firstName];
            [self.loginView addSubview:self.firstN];
            
            //lastname
            self.lastName=[[UILabel alloc]initWithFrame:CGRectMake(20, 50, 100, 30)];
            self.lastName.text=@"Last Name";
            [self.loginView addSubview:self.lastName];
            
            self.lastN=[[UILabel alloc]initWithFrame:CGRectMake(150, 50, 150, 30)];
            [self.lastN setText:lastName];
            [self.loginView addSubview:self.lastN];
            
            //gender
            self.gender=[[UILabel alloc]initWithFrame:CGRectMake(20, 100, 100, 30)];
            self.gender.text=@"Gender";
            [self.loginView addSubview:self.gender];
            
            self.genderN=[[UILabel alloc]init];
            self.genderN.frame=CGRectMake(150, 100, 150, 30);
            [self.mobileN setText:genderName];
            [self.loginView addSubview:self.genSeg];
            
            //mobile
            self.mobile=[[UILabel alloc]initWithFrame:CGRectMake(20, 150, 100, 30)];
            self.mobile.text=@"Mobile";
            [self.loginView addSubview:self.mobile];
            
            self.mobileN=[[UILabel alloc]initWithFrame:CGRectMake(150, 150, 150, 30)];
            [self.mobileN setText:mobile];
            [self.loginView addSubview:self.mobileN];
            
            //city
            self.city=[[UILabel alloc]initWithFrame:CGRectMake(20, 200, 100, 30)];
            self.city.text=@"City";
            [self.loginView addSubview:self.city];
            
            self.cityN=[[UILabel alloc]initWithFrame:CGRectMake(150, 200, 150, 30)];
            [self.cityN setText:city];
            [self.loginView addSubview:self.cityN];
            
            //email
            self.email=[[UILabel alloc]initWithFrame:CGRectMake(20, 250, 100, 30)];
            self.email.text=@"Email";
            [self.loginView addSubview:self.email];
            
            self.emailN=[[UILabel alloc]initWithFrame:CGRectMake(150, 250, 150, 30)];
            [self.emailN setText:email];
            [self.loginView addSubview:self.emailN];
        }
        
    }
    
}
-(void)agree
{
    if (self.agreeSeg.selectedSegmentIndex==0) {
        //submit
        self.submit.hidden=NO;
        self.submit=[UIButton buttonWithType:UIButtonTypeSystem];
        self.submit.frame=CGRectMake(150, 550, 100, 30);
        [self.submit setTitle:@"Submit" forState:UIControlStateNormal];
        [self.submit addTarget:self action:@selector(saving) forControlEvents:UIControlEventTouchUpInside];
        [self.userView addSubview:self.submit];
    }
    else if (self.agreeSeg.selectedSegmentIndex==1)
    {
        self.submit.hidden=YES;
    }
    
}
-(void)saving
{
    if (self.lastText.text.length>0 && self.firstText.text.length>0 && self.mobileText.text.length>0 && self.cityText.text.length>0 && self.emailText.text.length>0 && self.passwordText.text.length>0 && self.rePasswordText.text.length>0 && self.addressView.text.length>0) {
        
    NSString *gen=[self.genSeg titleForSegmentAtIndex:self.genSeg.selectedSegmentIndex];
        self.dic=[[NSMutableDictionary alloc]initWithObjects:@[self.firstText.text,self.lastText.text,gen,self.mobileText.text,self.cityText.text,self.addressView.text,self.emailText.text,self.passwordText.text,self.rePasswordText.text] forKeys:@[@"FN",@"LN",@"GN",@"MN",@"CN",@"AN",@"EN",@"PN",@"RN"]];
              
        [self.ary addObject:self.dic];
        [self.savingData setObject:self.emailText.text forKey:self.firstText.text];
        
        if ([self.passwordText.text isEqual:self.rePasswordText.text]) {
           NSLog(@"data submitted successfully");
        }
        else
        {
            NSLog(@"check password");
        }
    }
    else{
        NSLog(@"Fill all Details");
    }
    [self remove];
}
-(void)remove
{

    self.firstText.text=0;
    self.lastText.text=0;
    self.mobileText.text=0;
    self.cityText.text=0;
    self.emailText.text=0;
    self.passwordText.text=0;
    self.rePasswordText.text=0;
    self.addressView.text=0;
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (textField==self.firstText) {
        NSCharacterSet * firstNameTextSet=[NSCharacterSet characterSetWithCharactersInString:@"qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM"];
        for (int i=0; i<[string length]; i++) {
            unichar c = [string characterAtIndex:i];
            if (![firstNameTextSet characterIsMember:c]) {
                return NO;
            }
            
        }
//        if ([firstNameTextSet characterIsMember:c]) {
//            return YES;
//        }
//        
//    }
//        return NO;
       
        NSUInteger firstNameLength=self.firstText.text.length-range.length;
        return (firstNameLength<20);
    }
    if (textField==self.lastText) {
        NSCharacterSet *lastNameSet=[NSCharacterSet characterSetWithCharactersInString:@"qwertyuioplkjhgfdsazxcvbnmQAZWSXEDCFRVBGTYHNMJUIKLOP"];
        for (int i=0; i<[string length]; i++) {
            unichar c=[string characterAtIndex:i];
            if (![lastNameSet characterIsMember:c]) {
                return NO;
            }
        }
        NSUInteger lastNameLength=self.lastText.text.length-range.length;
        return (lastNameLength<20);
    }
    
    if (textField==self.mobileText) {
        NSCharacterSet *mobileSet=[NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
        for (int i=0; i<[string length]; i++) {
            unichar c=[string characterAtIndex:i];
            if (![mobileSet characterIsMember:c]) {
                return NO;
            }
        }
        NSUInteger mobileLength=self.mobileText.text.length-range.length;
        return (mobileLength<10);
    }
    
    if (textField==self.cityText) {
        NSCharacterSet *citySet=[NSCharacterSet characterSetWithCharactersInString:@"qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM"];
        for (int i=0; i<[string length]; i++) {
            unichar c=[string characterAtIndex:i];
            if (![citySet characterIsMember:c]) {
                return NO;
            }
        }
        NSUInteger cityLength=self.cityText.text.length-range.length;
        return (cityLength<10);
    }
    
    if (textField==self.emailText) {
        NSCharacterSet *emailSet=[NSCharacterSet characterSetWithCharactersInString:@"qwertyuioplkjhgfdsazxcvbnmMNBVCXZASDFGHJKLPOIUYTREWQ!@_.1023654789"];
        for (int i=0; i<[string length]; i++) {
            unichar c=[string characterAtIndex:i];
            if (![emailSet characterIsMember:c]) {
                return NO;
            }
        }
        NSUInteger emailLength=self.emailText.text.length-range.length;
        return (emailLength<30);
    }
    
    if (textField==self.passwordText) {
        NSCharacterSet *passwordSet=[NSCharacterSet characterSetWithCharactersInString:@"1236547890QAWSEDRFTGYHUJIKOLPMNBVXZCqwertyuioplkjhgfdsazxcvbnm./*-+@#$%^&!"];
        for (int i=0; i<[string length]; i++) {
            unichar c=[string characterAtIndex:i];
            if (![passwordSet characterIsMember:c]) {
                return NO;
            }
        }
        NSUInteger passwordLength=self.passwordText.text.length-range.length;
        return (passwordLength<20);
    }
    
    if (textField==self.rePasswordText) {
        NSCharacterSet *rePasswordSet=[NSCharacterSet characterSetWithCharactersInString:@"1236547890QAWSEDRFTGYHUJIKOLPMNBVXZCqwasedrftgyhujikolpmnbvcxz./*-+@#$%^&!"];
        for (int i=0; i<[string length]; i++) {
            unichar c=[string characterAtIndex:i];
            if (![rePasswordSet characterIsMember:c]) {
                return NO;
            }
        }
        NSUInteger rePasswordLength=self.rePasswordText.text.length-range.length;
        return (rePasswordLength<20);
    }
    
    return YES;
}

@end
Previous
Next Post »