UIAlertController使用
来源:互联网 发布:发生未知的网络错误 编辑:程序博客网 时间:2024/05/17 08:11
UIAlertController基本可以简单的理解为UIAlertController == UIAlertView + UIActionSheet,相比后面两个100%的开发朋友使用过.先简单看下UIAlertView和UIActionSheet的使用和效果
1.UIAlertView使用:
UIAlertView*alert = [[UIAlertView alloc]initWithTitle:@"提示"message:@"我是最牛逼的开发"delegate:nilcancelButtonTitle:@"不是"otherButtonTitles:@"是",nil];alert.alertViewStyle=UIAlertViewStyleLoginAndPasswordInput;[alert show];
2.UIActionSheet使用
UIActionSheet*sheet = [[UIActionSheetalloc]initWithTitle:@"我是最牛逼的开发"delegate:nilcancelButtonTitle:@"是"destructiveButtonTitle:@"是" otherButtonTitles:@"关闭",nil];[sheet showInView:self.view];
以上的使用,在熟悉不过了,就不多说了。那么说说UIAlertController吧,直接先看看使用,然后再说说区别
3.UIAlertController使用
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示"message:@"我是最牛逼的发"preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:alertanimated:YEScompletion:nil];
当你想 show出来的时候,进入UIAlertController.h文件发现该类没有 show方法,但是看到@interface UIAlertController :UIViewController 从这就看出来该类是一个控制器。那么我们就可以试着想普通展示一个UIViewController的方法试一试,在这我使用的是model
运行看效果:
这个样子,傻眼了,它挡住了整个屏幕,又没有关闭按钮,导致无法关闭。那么如何添加像UIAlertView一样添加一些取消或者其他按钮。UIAlertController是通过
- (void)addAction:(UIAlertAction*)action; 这样添加一个action就是添加一个按钮事件,就像这样:
[alert addAction [UIAlertActionactionWithTitle:@"确定"style:UIAlertActionStyleDestructivehandler:^(UIAlertAction*action) { NSLog(@"点击了确定按钮"); }]];[alert addAction:[UIAlertActionactionWithTitle:@"取消"style:UIAlertActionStyleCancelhandler:^(UIAlertAction*action) { NSLog(@"点击了取消按钮"); }]];
我们看到,它看起来正常工作了,需要注意的是,action事件的处理在添加是采用了Block方式添加进去,个人觉得这样更加出色。当在同一个类中创建多个UIAlertController对象时,还要像之前一样在代理种区分具体对象,或者采用runtime发发为每一个UIAlertController对象,通过下面的方法
objc_setAssociatedObject 以及 objc_getAssociatedObject
添加自己的具体操作,触发时,取出具体操作。另外在添加了按钮后,那么如何添加UITextField文本框。UIAlertController是采用
[alert addTextFieldWithConfigurationHandler:^(UITextField*textField) { textField.textColor= [UIColorredColor];textField.text=@"iOS8";[textField addTarget:selfaction:@selector(usernameDidChange:)forControlEvents:UIControlEventEditingChanged];}];
这样,就可以添加输入文本框了,就像这样
从上面的代码,我在添加时,还对textField进行了一些设置,比如默认文字以及textColor。并且对textField添加了值改变的监听事件。各种处理,我们都可以脑洞大开。
在一开始,说了基本可以认为UIAlertController == UIAlertView + UIActionSheet 上面演示的都是Alert,那么ActionSheet的方式呢,如下,只需要在创建代码
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示"message:@"我是最牛逼的发"preferredStyle:UIAlertControllerStyleActionSheet];
创建时将UIAlertControllerStyleAlert 改为UIAlertControllerStyleActionSheet 。alert就可以在底部弹出了,效果各位可以自己运行一下看看。需要注意的是:在UIAlertControllerStyleActionSheet样式下,是不可以添加UITextField。否则就会报错。
4.简单的区别
1.UIAlertView iOS系统为了保证UIAlertView在所有界面之上,它会临时创建一个新的UIWindow,通过将UIWindow的UIWindowLevel设置的更高,让UIAlertView盖在所有应用的界面之上
2.UIAlertController 是继承自UIViewController,它采用一个UIPopoverPresentationController类进行管理,UIPopoverPresentationController又继承自 UIPresentationController,其中的presentingViewController属性表示展示之前的Controller,presentedViewController属性表示被展示的Controller。另外这种方式,也统一了iPhone和iPad的使用方式
- UIAlertController使用
- UIAlertController 使用
- UIAlertController使用
- UIAlertController使用
- UIAlertController使用
- UIAlertController使用
- UIAlertController使用
- UIAlertController使用
- UIAlertController的使用(Swift)
- UIAlertController的使用示例
- UIAlertController的使用二
- UIAlertController的使用
- UIAlertController的使用
- 关于UIAlertController的使用
- UIAlertController的使用
- UIAlertController 的使用
- UIAlertController 的使用
- UIAlertController的使用
- 有的笔记本电脑,可以做工作站什么意思?
- ansj分词史上最详细教程
- 【Oracle RAC+DG实验】Oracle RAC+ASM+DataGuard配置实验记录+常见问题
- ZooKeeper 命令操作
- Retrofit使用指南
- UIAlertController使用
- WinTel联盟发展史
- Android编程思想,面向对象程序设计第六篇——封装(上)布局模块化
- 存储管理之分页存储管理方式C语言模拟(实验五)
- C语言
- @RequestParam @RequestBody @PathVariable 等参数绑定注解详解
- ARP地址解析协议
- 字符串、单词首字母转化为大写
- LinkedHashSet去重方法