UIAlertView和UIAlertViewController

 1 #import "ViewController.h"
 2 
 3 @interface ViewController ()<UIAlertViewDelegate>
 4 
 5 @end
 6 
 7 @implementation ViewController
 8 
 9 - (void)viewDidLoad {
10     [super viewDidLoad];
11     // Do any additional setup after loading the view, typically from a nib.
12 }
13 - (IBAction)didClickButtonAction:(UIButton *)sender {
14     
15     //版本判断
16     float version = [UIDevice currentDevice].systemVersion.floatValue;
17     if (version < 8.0) {
18         
19         //iOS8.0以前UIAlertView,用show展示
20             UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"密码错误" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定",@"孟德峰",@"原溢凯", nil];
21             //弹出alertView
22             [alertView show];
23         
24     }else{
25         //iOS8.0以后UIAlertController,用模态展示
26         UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"密码错误" preferredStyle:UIAlertControllerStyleActionSheet];
27         //添加点击按钮
28         [alertC addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
29             NSLog(@"点击确定");
30         }]];
31         [self presentViewController:alertC animated:YES completion:nil];
32     }
33     
34 }
35 //代理方法,判断点击了那个按钮
36 -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
37 {
38     NSLog(@"%ld",buttonIndex);
39 }
原文地址:https://www.cnblogs.com/DevinSMR/p/5285050.html