代码如下:
1 2 3 4 5 6 7 8 9
| UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"My Alert" message:@"This is an alert." preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {}]; [alert addAction:defaultAction]; [self presentViewController:alert animated:YES completion:nil];
|
UIAlertControllerStyle
其中 UIAlertControllerStyle
枚举效果如下:
UIAlertActionStyle
- UIAlertActionStyleDefault
- UIAlertActionStyleCancel
- UIAlertActionStyleDestructive
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"My Alert message" message:@"This is an alert" preferredStyle:(UIAlertControllerStyleActionSheet)];
UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"OK" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) { }];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"OK" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) { }];
UIAlertAction *destructiveAction = [UIAlertAction actionWithTitle:@"OK" style:(UIAlertActionStyleDestructive) handler:^(UIAlertAction * _Nonnull action) { }];
[alert addAction:defaultAction]; [alert addAction:cancelAction]; [alert addAction:destructiveAction];
[self presentViewController:alert animated:YES completion:^{ }];
|
UIAlertActionStyleCancel 类型的 UIAlertAction 对象只能有一个,其他类型的 UIAlertAction 对象可以有多个