Showing posts with label AlertController related.... Show all posts
Showing posts with label AlertController related.... Show all posts

27 Nov 2017

How to Create a Global UIAlertController in Swift...


 Add this code in any Class (As UIViewController extension)  as shown below

extension UIViewController {
    
    func alert(message: String, title: String ) {
        let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
        let OKAction = UIAlertAction(title: "OK", style: .default, handler: nil)
        alertController.addAction(OKAction)
        self.present(alertController, animated: true, completion: nil)
    }
    

}


and call it anywhere(In  Any ViewController) as shown below,

   
self.alert(message: "Some message", title: "Title of Alert")

28 Mar 2017

ActionSheet (AlertController) example...



ActionSheet (AlertController) example...









- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.    
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(120, 150, 60, 40);
    btn.backgroundColor = [UIColor redColor];
    btn.layer.borderWidth = 2;
    btn.layer.borderColor = [UIColor greenColor].CGColor;
    [btn setTitle:@"Press" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    btn.layer.cornerRadius = 10;
    [btn addTarget:self action:@selector(btnAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    

}

- (void)btnAction {
   
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Directions"
                                                                   message:@"Select mode of transportation"
                                                            preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *drivingAction = [UIAlertAction actionWithTitle:@"Driving" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        // this block runs when the driving option is selected
        NSLog(@"Clicked on Driving");
    }];
    UIAlertAction *walkingAction = [UIAlertAction actionWithTitle:@"Walking" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        // this block runs when the walking option is selected
        NSLog(@"Clicked on Walking");
    }];
    
    UIAlertAction *flyingAction = [UIAlertAction actionWithTitle:@"Flying" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        // this block runs when the walking option is selected
        NSLog(@"Clicked on Flying");
    }];
    
    UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil];

    [alert addAction:drivingAction];
    [alert addAction:walkingAction];
    [alert addAction:flyingAction];
    [alert addAction:defaultAction];
   
    [self presentViewController:alert animated:YES completion:nil];
}

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:





16 Mar 2017

AlertController with Table View & with Label inside it...



AlertController with Table View & with Label inside it...



StoryBoard...




#import <UIKit/UIKit.h>

@interface ViewController : UIViewController


- (IBAction)alertWithLabelBtnTapped:(id)sender;
- (IBAction)alertWithTVBtnTapped:(id)sender;

@end



#import "ViewController.h"



@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>

@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)alertWithLabelBtnTapped:(id)sender {
    
    UIViewController *controller = [[UIViewController alloc]init];
    CGRect rect = CGRectMake(0, 0, 180, 50);
    [controller setPreferredContentSize:rect.size];
    UILabel *alertLbl = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 180, 40)];
    alertLbl.backgroundColor = [UIColor orangeColor];
    alertLbl.text = @" I am Alert Label";
    alertLbl.textColor = [UIColor greenColor];
    [controller.view addSubview:alertLbl];
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title" message:@"Message" preferredStyle:UIAlertControllerStyleAlert];
    [alertController setValue:controller forKey:@"contentViewController"];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
        
    }];
    [alertController addAction:cancelAction];
    [self presentViewController:alertController animated:YES completion:nil];

}

- (IBAction)alertWithTVBtnTapped:(id)sender {
    
    UIViewController *controller = [[UIViewController alloc]init];
    UITableView *alertTableView;
    
    CGRect rect = CGRectMake(0, 0, 280, 250);
    [controller setPreferredContentSize:rect.size];
    alertTableView  = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 280, 250)];
    
    alertTableView.delegate = self;
    alertTableView.dataSource = self;
    alertTableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
    [alertTableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
    [alertTableView setTag:1];
    [controller.view addSubview:alertTableView];
    [controller.view bringSubviewToFront:alertTableView];
    [controller.view setUserInteractionEnabled:YES];
    [alertTableView setUserInteractionEnabled:YES];
    [alertTableView setAllowsSelection:YES];
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title" message:@"Message" preferredStyle:UIAlertControllerStyleAlert];
    [alertController setValue:controller forKey:@"contentViewController"];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
        
    }];
    [alertController addAction:cancelAction];
    [self presentViewController:alertController animated:YES completion:nil];

    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 8;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *MyIdentifier = @"CellID";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:MyIdentifier] ;
    }
        cell.textLabel.text = @"Hellooo";
   
    return cell;
}


@end


Results:


Recent Posts

Codable demo

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