17 Mar 2017

AlertController With Textfields to login Validate...


AlertController With Textfields to login Validate...

Storyboard:


#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

- (IBAction)loginButtonTapped:(id)sender;

@end



#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
}


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

- (IBAction)loginButtonTapped:(id)sender {
    
    UIAlertController * alertController = [UIAlertController alertControllerWithTitle: @"Login"
                                                                              message: @"Input username and password"
                                                                       preferredStyle:UIAlertControllerStyleAlert];
  
    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        textField.placeholder = @"name";
        textField.textColor = [UIColor blueColor];
        textField.clearButtonMode = UITextFieldViewModeWhileEditing;
        textField.borderStyle = UITextBorderStyleRoundedRect;
    }];
    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        textField.placeholder = @"password";
        textField.textColor = [UIColor blueColor];
        textField.clearButtonMode = UITextFieldViewModeWhileEditing;
        textField.borderStyle = UITextBorderStyleRoundedRect;
        textField.secureTextEntry = YES;
    }];
    
    UIAlertAction *loginAction = [UIAlertAction actionWithTitle:@"LOGIN" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        NSArray * textfields = alertController.textFields;
        UITextField * namefield = textfields[0];
        UITextField * passwordfiled = textfields[1];
        NSLog(@"%@:%@",namefield.text,passwordfiled.text);
        
        if ([namefield.text  isEqual: @"ravindar"] & [passwordfiled.text  isEqual: @"ravindar"]) {
            [alertController dismissViewControllerAnimated:YES completion:nil];
            
            // Write Code here to do whatever after Succesfull Login.....
            
        }else{
            [self presentViewController:alertController animated:YES completion:nil];
        
//            [alertController setTitle:@"Invalid Login"];
//            [alertController setMessage:@"Enter Correct Username or Password"];
            
            /* Above default Title & message is Present bt Over it added a View of label */
            UILabel *alertLbl = [[UILabel alloc]initWithFrame:CGRectMake(8, 8, 250, 55)];
            alertLbl.backgroundColor = [UIColor whiteColor];
            alertLbl.numberOfLines = 0;
            alertLbl.textAlignment = 1; //From Left 0, middle 1, Rught 2
            alertLbl.text = @"Enter Correct Username or Password";
            alertLbl.textColor = [UIColor redColor];
            [alertController.view addSubview:alertLbl];
            
            alertController.view.tintColor = [UIColor redColor]; // Action Title Color
        }
        
    }];

     [alertController addAction:loginAction];
     [self presentViewController:alertController animated:YES completion:nil];
//     loginAction.enabled = NO;

}

@end


Results:





No comments:

Post a Comment

Recent Posts

Codable demo

Link: https://www.dropbox.com/s/kw7c1kgv1628bh7/codableDemo.zip?dl=0