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.
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
UIAlertController *alertView = [UIAlertController alertControllerWithTitle:@"Menu" | |
message:@"Select a menu" | |
preferredStyle:UIAlertControllerStyleAlert]; |
- Adding an Action
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[alertView addAction:[UIAlertAction actionWithTitle:@"ok" | |
style:UIAlertActionStyleDefault | |
handler:^(UIAlertAction *action) { | |
//todo | |
}]]; |
The handler block can be used to get the call back when “ok” button is pressed.
- Presenting the alertController
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[self presentViewController:alertView animated:YES completion:^{ | |
NSLog(@"alertView shown!"); | |
}]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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!"); | |
}]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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!"); | |
}]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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"); | |
}]; | |
} |
The Alert will be looking like as given below.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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"); | |
}]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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!"); | |
}]; | |
} |
The ActionSheet will be looking like as given below.
Good.
ReplyDeleteThanks.
Thank you.
ReplyDelete