IOS Development UIAlertController Detailed Explanation and Example Code
- 2021-08-21 21:38:09
- OfStack
Detailed Explanation of UIAlertController Development by IOS
After iOS 8.0, Apple abandoned UIAlertView and UIActionSheet, and instead used UIAlertController to integrate the previous UIAlertView and UIActionSheet into one. The new version of API has become concise, and a few lines of code can realize the functions of the previous 1 large piece of code
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"My Alert"
message:@"This is an alert."
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
NSLog(@" Hello, hello ");
}];
UIAlertAction* defaultAction2 = [UIAlertAction actionWithTitle:@"OK2" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
NSLog(@" Hello, hello ");
}];
[alert addAction:defaultAction];
[alert addAction:defaultAction2];
[self presentViewController:alert animated:YES completion:nil];
Initializing AlertView doesn't make much difference, the main difference is adding events. Apple has added UIAlertAction to add events. One Action corresponds to one event, which can be used when added to alert.
Switching to ActionSheet only requires modifying preferredStyle to UIAlertControllerStyleActionSheet
You can also add the following input box code
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @" Enter a user name ";
}];
Thank you for reading, hope to help everyone, thank you for your support to this site!