Wednesday, 1 October 2014

How to use UIAlertController instead of UIAlertView & UIActionsheet in iOS8


In iOS8 UIAlertController replaces UIActionsheet and UIAlertView. An AlertController can be created with a "title" and a "message".

We can set two styles to an AlertController. Which is given below.
(a)UIAlertControllerStyleActionSheet -  for configuring UIAlertController as an ActionSheet
(b)UIAlertControllerStyleAlert             -  for configuring UIAlertController as an Alert.

How to create an Alert/ActionSheet
  • Creating an AlertController object

UIAlertController *alertView = [UIAlertController alertControllerWithTitle:@"Menu"
message:@"Select a menu"
preferredStyle:UIAlertControllerStyleAlert];
view raw gistfile1.txt hosted with ❤ by GitHub

  • Adding an Action

[alertView addAction:[UIAlertAction actionWithTitle:@"ok"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
//todo
}]];
view raw gistfile1.txt hosted with ❤ by GitHub
      
The handler block can be used to get the call back when “ok” button is pressed.
  • Presenting the alertController

[self presentViewController:alertView animated:YES completion:^{
NSLog(@"alertView shown!");
}];
view raw gistfile1.txt hosted with ❤ by GitHub
How to create a simple Alert with OK button and a message

The following method will create a simple Alert in iOS8.

- (void)showSimpleAlert {
UIAlertController *alertView = [UIAlertController alertControllerWithTitle:@"Alert!"
message:@"This is an Alert."
preferredStyle:UIAlertControllerStyleAlert];
[alertView addAction:[UIAlertAction actionWithTitle:@"ok"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
NSLog(@"action - %@", action.title);
}]];
[self presentViewController:alertView animated:YES completion:^{
NSLog(@"alertView shown!");
}];
}
view raw gistfile1.txt hosted with ❤ by GitHub
The Alert will be looking like as given below.




How to create a an Alert with two buttons

- (void)showAlertWithTwoButtons {
UIAlertController *alertView = [UIAlertController alertControllerWithTitle:@"Alert!"
message:@"This is an Alert."
preferredStyle:UIAlertControllerStyleAlert];
[alertView addAction:[UIAlertAction actionWithTitle:@"ok"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
NSLog(@"action - %@", action.title);
}]];
[alertView addAction:[UIAlertAction actionWithTitle:@"Cancel"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
NSLog(@"action - %@", action.title);
}]];
[self presentViewController:alertView animated:YES completion:^{
NSLog(@"alertView shown!");
}];
}
view raw gistfile1.txt hosted with ❤ by GitHub
The Alert will be looking like as given below.





How to create an Alert with a TextField

- (void)showAlertWithTextField {
UIAlertController *alertView = [UIAlertController alertControllerWithTitle:@"Menu"
message:@"Select a menu"
preferredStyle:UIAlertControllerStyleAlert];
[alertView addTextFieldWithConfigurationHandler:^(UITextField *textField) {
//Customize the textField
textField.placeholder = @"User name";
textField.textColor = [UIColor whiteColor];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.backgroundColor = [UIColor colorWithRed:171/255 green:182/255 blue:255/255 alpha:0.5];
}];
[alertView addAction:[UIAlertAction actionWithTitle:@"ok"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
NSLog(@"action - %@", action.title);//callback
}]];
[self presentViewController:alertView animated:YES completion:^{
NSLog(@"completed");
}];
}
view raw gistfile1.txt hosted with ❤ by GitHub

The Alert will be looking like as given below.




How to create an Alert with two textfields

- (void)showAlertWithTwoTextField {
UIAlertController *alertView = [UIAlertController alertControllerWithTitle:@"Menu"
message:@"Select a menu"
preferredStyle:UIAlertControllerStyleAlert];
[alertView addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @"User name";
textField.textColor = [UIColor whiteColor];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.backgroundColor = [UIColor colorWithRed:171/255 green:182/255 blue:255/255 alpha:0.5];
}];
[alertView addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @"Passcode";
textField.textColor = [UIColor whiteColor];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.backgroundColor = [UIColor colorWithRed:171/255 green:182/255 blue:255/255 alpha:0.5];
}];
[alertView addAction:[UIAlertAction actionWithTitle:@"ok"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
NSLog(@"action - %@", action.title);
}]];
[self presentViewController:alertView animated:YES completion:^{
NSLog(@"completed");
}];
}
view raw gistfile1.txt hosted with ❤ by GitHub
The Alert will be looking like as given below.




How to create an Actionsheet with two buttons

For creating an ActionSheet use "PreferredStyle" as "UIAlertControllerStyleActionSheet".

- (void)showActionSheetWithTwoButtons {
UIAlertController *alertView = [UIAlertController alertControllerWithTitle:@"ActionSheet!"
message:@"This is an ActionSheet."
preferredStyle:UIAlertControllerStyleActionSheet];
[alertView addAction:[UIAlertAction actionWithTitle:@"ok"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
NSLog(@"action - %@", action.title);
}]];
[alertView addAction:[UIAlertAction actionWithTitle:@"Cancel"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
NSLog(@"action - %@", action.title);
}]];
[self presentViewController:alertView animated:YES completion:^{
NSLog(@"alertView shown!");
}];
}
view raw gistfile1.txt hosted with ❤ by GitHub

The ActionSheet will be looking like as given below.