Sunday, June 11, 2017

Showing Alerts using UIAlertController

Hi guys, we’ll just look on how alerts are created in iOS 8 using UIAlertViewController. Previously you may have created alerts using UIAlertView. When using UIAlertView, you had to add buttons and UIAlertViewDelegate methods to create callback methods to do some actions when those buttons were clicked.

UIAlertController allows you to create alert and add buttons and block based callback code to handle button actions. Look on the following code to see how this can be done.

Syntax:


+ (instancetype)alertControllerWithTitle:(NSString *)title
                                message:(NSString *)message
                         preferredStyle:(UIAlertControllerStyle)preferredStyle;

Title and message are the content shown in the alert as strings. And the preferred style can be UIAlertControllerStyleAlert or UIAlertControllerStyleActionSheet to decide whether the view should be shown as alert or actionsheet.



UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"My App"
                              message:@"Give your name to validate"
                              preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
  handler:^(UIAlertAction * action) {}];

[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];




Like the above, buttons can be added with block based actions. Buttons are shown in the alert as in the order how the actions are added. Only one Cancel button can be added per alert.

The alert action styles are specified as:

  • UIAlertActionStyleDefault
  • UIAlertActionStyleCancel
  • UIAlertActionStyleDestructive


UIAlertAction *cancelAction = [UIAlertAction
           actionWithTitle:NSLocalizedString(@"Cancel", @"Cancel action")
                     style:UIAlertActionStyleCancel
                   handler:^(UIAlertAction *action)
                   {
                     NSLog(@"Cancel action");
                   }];

UIAlertAction *okAction = [UIAlertAction
           actionWithTitle:NSLocalizedString(@"OK", @"OK action")
                     style:UIAlertActionStyleDefault
                   handler:^(UIAlertAction *action)
                   {
                     NSLog(@"OK action");
                   }];

UIAlertAction *clearAction = [UIAlertAction
            actionWithTitle:NSLocalizedString(@"Clear", @"Clear action")
                      style:UIAlertActionStyleDestructive
                    handler:^(UIAlertAction *action)
                    {
                      NSLog(@"Clear action");
                    }];

[alertController addAction:cancelAction];
[alertController addAction:okAction];
[alertController addAction:clearAction];



AlertController with TextFields to input:
Textfields to get input through alert can be done by this code.

[alert addTextFieldWithConfigurationHandler:^(UITextField *textField){
textField.placeholder = @"userName";

}];

This adds a textfield to the alertcontroller with the placeholder “userName”. You can add many text inputs and action buttons. 
The input text given in the alertcontroller can be received as follows:

UIAlertAction *okAction = [UIAlertAction
                              actionWithTitle:NSLocalizedString(@"OK", @"OK action")
                              style:UIAlertActionStyleDefault
                              handler:^(UIAlertAction *action)
                              {
                                  UITextField *userName = alert.textFields.firstObject;
                                  
                                  NSLog(@"username: %@", userName.text);
                                  
                              }];
[alert addAction:okAction];    

[self presentViewController:alert animated:YES completion:nil];


1 comment:

  1. Great tutorial for beginners sir, It helped me lot to gain knowledge in AlertController.
    Thanks

    ReplyDelete