UIAlertView和UIAlertViewController

本文介绍了在iOS开发中如何根据不同版本使用UIAlertView或UIAlertController来显示警告框,并提供了具体的代码实现方式。
 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值