Quantcast
Channel: hayaGeek » Objective-C
Viewing all articles
Browse latest Browse all 10

UIAlertController Example in iOS

$
0
0

In UIAlertController Example, I have covered how to use UIAlertController in iOS.
UIAlertController is introduced in iOS 8  and supports two styles. Using this you can create an alert dialog(UIAlerview) or action sheet(UIActionSheet)

typedef NS_ENUM(NSInteger, UIAlertControllerStyle) {
    UIAlertControllerStyleActionSheet = 0,
    UIAlertControllerStyleAlert
} NS_ENUM_AVAILABLE_IOS(8_0);

Below are the examples covered in this article
1) Simple Alert Dialog
2) Alert Dialog with actions
3) Action sheet with actions
4) Create an alert dialog with Textfields

Useful API in UIAlertController

Below are the useful methods in UIAlertController.

//Create UIAlertController
+ (instancetype)actionWithTitle:(NSString *)title style:(UIAlertActionStyle)style 
							handler:(void (^)(UIAlertAction *action))handler;


//Adding an action
- (void)addAction:(UIAlertAction *)action;


//Adding text filed to UIAlertController.This method is supported only for  UIAlertControllerStyleAlert
- (void)addTextFieldWithConfigurationHandler:(void (^)(UITextField *textField))configurationHandler;

 

1) Create a Simple Alert Dialog

Simple UIAlertController Alert

Using the below code you can create UIAlertController with style UIAlertControllerStyleAlert  and present it.

UIAlertController * alert=   [UIAlertController
                                    alertControllerWithTitle:@"My Title"
                                    message:@"Enter User Credentials"
                                    preferredStyle:UIAlertControllerStyleAlert];
    
    [self presentViewController:alert animated:YES completion:nil];

 

2) Create An Alert Dialog with actions

UIAlertController example With Alert

To add OK/Cancel Button to dialog, You need to create an action and add it it to UIAlertController.

You can create an action using the following code:

    UIAlertAction* ok = [UIAlertAction
                         actionWithTitle:@"OK"
                         style:UIAlertActionStyleDefault
                         handler:^(UIAlertAction * action)
                         {
                             //Do some thing here
                             [view dismissViewControllerAnimated:YES completion:nil];
                             
                         }];
    [alert addAction:ok]; // add action to uialertcontroller

Below is the UIAlertController Example with acctions:

 UIAlertController * alert=   [UIAlertController
                                  alertControllerWithTitle:@"Info"
                                  message:@"You are using UIAlertController"
                                  preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction* ok = [UIAlertAction
                         actionWithTitle:@"OK"
                         style:UIAlertActionStyleDefault
                         handler:^(UIAlertAction * action)
                         {
                             [alert dismissViewControllerAnimated:YES completion:nil];
                             
                         }];
    UIAlertAction* cancel = [UIAlertAction
                             actionWithTitle:@"Cancel"
                            style:UIAlertActionStyleDefault
                            handler:^(UIAlertAction * action)
                            {
                                [alert dismissViewControllerAnimated:YES completion:nil];
                                                       
                            }];
    
    [alert addAction:ok];
    [alert addAction:cancel];
    
    [self presentViewController:alert animated:YES completion:nil];

 

3) Create an Action sheet with actions

UIAlertController ActionSheet Example

 UIAlertController * view=   [UIAlertController
                                  alertControllerWithTitle:@"My Title"
                                  message:@"Select you Choice"
                                  preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction* ok = [UIAlertAction
                         actionWithTitle:@"OK"
                         style:UIAlertActionStyleDefault
                         handler:^(UIAlertAction * action)
                         {
                             //Do some thing here
                             [view dismissViewControllerAnimated:YES completion:nil];
                             
                         }];
    UIAlertAction* cancel = [UIAlertAction
                             actionWithTitle:@"Cancel"
                             style:UIAlertActionStyleDefault
                             handler:^(UIAlertAction * action)
                             {
                                 [view dismissViewControllerAnimated:YES completion:nil];
                                 
                             }];
    
    
    [view addAction:ok];
    [view addAction:cancel];
    [self presentViewController:view animated:YES completion:nil];

 

4) Create an Alert dialog with username and password fields.

To add a text field you can use the method addTextFieldWithConfigurationHandler.
Text fields can be added to only type UIAlertControllerStyleAlert.

    [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        textField.placeholder = @"Password"; //for passwords
        textField.secureTextEntry = YES;
    }];

Using the following code, You can create an alert dialog like UIAlertView with style UIAlertViewStyleLoginAndPasswordInput

UIAlertControler Username Passwrod

UIAlertController * alert=   [UIAlertController
                                  alertControllerWithTitle:@"My Title"
                                  message:@"Enter User Credentials"
                                  preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
                                               handler:^(UIAlertAction * action) {
                                                   //Do Some action here
                                                   
                                               }];
    UIAlertAction* cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault
                                                   handler:^(UIAlertAction * action) {
                                                       [alert dismissViewControllerAnimated:YES completion:nil];
                                                   }];
    
    [alert addAction:ok];
    [alert addAction:cancel];
    
    [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        textField.placeholder = @"Username";
    }];
    [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        textField.placeholder = @"Password";
        textField.secureTextEntry = YES;
    }];
    
    [self presentViewController:alert animated:YES completion:nil];


Reference:Apple Documenation

The post UIAlertController Example in iOS appeared first on hayaGeek.


Viewing all articles
Browse latest Browse all 10

Trending Articles