How to show alert controller

UIAlertController Example

UIAlertController is introduced in iOS 8  and it supports two styles. Using this you can create an alert dialog like UIAlerview or action sheet like UIActionSheet.

1) SIMPLE ALERT DIALOG

Using the below code you can show alert with without any button

UIAlertController * alert= [UIAlertController alertControllerWithTitle:@"Alert" message:@"Thank you using alert controller" preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:alert animated:YES completion:nil];

2) SIMPLE ALERT DIALOG WITH ONE BUTTON

Using the below code you can show alert with one button

UIAlertController * alert= [UIAlertController alertControllerWithTitle:@"Alert" message:@"Thank you using alert controller" preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil]];
[self presentViewController:alert animated:YES completion:nil];

3) SIMPLE ALERT DIALOG WITH TWO BUTTONS

Using the below code you can show alert with two buttons with action

UIAlertController * alert= [UIAlertController alertControllerWithTitle:@"Alert" message:@"Thank you using alert controller" preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil]];
[alert addAction:[UIAlertAction actionWithTitle:@"Login" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action){
 //your code here
 }]];
[self presentViewController:alert animated:YES completion:nil];

4) CREATE AN ACTION SHEET WITH ACTIONS

Using the below code you can show actionsheet with two buttons with action

UIAlertController * alert= [UIAlertController alertControllerWithTitle:@"Action sheet" message:@"Thank you using action sheet" preferredStyle:UIAlertControllerStyleActionSheet];
[alert addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil]];
[alert addAction:[UIAlertAction actionWithTitle:@"Login" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action){
 //your code here
 }]];
[self presentViewController:alert animated:YES completion:nil];

5) 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.

UIAlertController * alert= [UIAlertController alertControllerWithTitle:@"Alert" message:@"Enter your login Credentials" preferredStyle:UIAlertControllerStyleAlert];
[alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
 textField.placeholder = @"Username"; //for username
 }];
[alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
 textField.placeholder = @"Password"; //for passwords
 textField.secureTextEntry = YES;
 }];
[alert addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil]];
[alert addAction:[UIAlertAction actionWithTitle:@"Login" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action){
 //your code here
 }]];
[self presentViewController:alert animated:YES completion:nil];

You can find Swift Version here

Reference: Apple Documenation

That’s It. If you like this post, Please comment below or subscribe this blog.

Leave your comments

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s