UIAlertController的使用
在iOS8中,苹果对UIAlertView和UIActionSheet进行了重新的封装,成为适应性更强,灵活性更高的UIAlertController。具体使用方法如下。
UIAlertController具有两种风格,即
UIAlertControllerStyleActionSheet
UIAlertControllerStyleAlert
至于其使用方法也是非常简单得 ,代码如下
UIAlertController*alert=[UIAlertController alertControllerWithTitle:@"警告框" message:nil preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
NSLog(@"确定");
}]];
[alert addAction:[UIAlertAction actionWithTitle:@"警示性" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
NSLog(@"不确定");
}]];
[alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
NSLog(@"取消");
}]];
[self presentViewController:al animated:YES completion:nil];
以上代码,既可以实现UIAlertView也可以实现ActionSheet, 只需要修改初始化的第三个参数,便可实现相应得弹窗。
如果是UIAlertControllerStyle当然还可以添加文本框
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder=@"一个文本框";
}];
如果要读取文本框中的内容
[alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
UITextField * textField=al.textFields.firstObject;
NSLog(@"%@",textField.text);
}]];
以上便是UIAlertController的使用方法,但是,当我们在使用UIAlertControllerStyleActionSheet时需要注意,通过以上方法,我们将actionSheet运行在iPhone这样的设备上是没有问题得,但是如果运行在大屏得ipad上便会出现崩溃。 这是因为弹出框必须要有一个锚点,即 anchor point。当我们在iPhone等常规屏幕的设备上使用actionSheet时,很明显,锚点在屏幕下方,而在大屏得ipad上,则需要我们自己手动设置。只需要加入以下代码
UIPopoverPresentationController * popover=al.popoverPresentationController;
UIButton * button=(UIButton *)sender;
if (popover) {
popover.sourceRect=button.bounds;
popover.sourceView=button;
popover.permittedArrowDirections=UIPopoverArrowDirectionAny;
}
另外,当我们在使用此方法后,ipad上加入的UIAlertActionStyleCancel风格的按钮是不显示,即默认去掉了取消按钮。
UIAlertController的使用的更多相关文章
- UIAlertController
楼主在整理项目的警告,于是乎你懂的. 然后自己整理了一下以后方便自己忘了之后能及时找到它 关于UIAlertController .h文件的解析 /** 关于UIAlertController的解析 ...
- iOS UIAlertController跟AlertView用法一样 && otherButtonTitles:(nullable NSString *)otherButtonTitles, ... 写法
今天写弹出框UIAlertController,用alertView习惯了,所以封装了一下,跟alertView用法一样,不说了,直接上代码: 先来了解一下otherButtonTitles:(nul ...
- UIAlertController使用
// 将UIAlertController模态出来 相当于UIAlertView show 的方法// 初始化一个一个UIAlertController // 参数preferredStyle: ...
- IOS UIAlertController 使用方法
在很多种语言中,alert都表示弹窗操作,弹窗功能非常有用,不仅可以用于正式的app功能中,也可以在调试中使用.在OC中,UIAlertController类用来控制弹窗操作.在IOS 8.0之前, ...
- UI控件(UIAlertController)
@implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; UIButton *_button = [UIBut ...
- UIAlertController 部分用法及属性
//创建UIAlertController:初始化UIAlertController 需要使用alertControllerWithTitle UIAlertController *alertCont ...
- iOS--UIAlertView与UIAlertController和UIAlertAction之间的事儿
iOS 8的新特性之一就是让接口更有适应性.更灵活,因此许多视图控制器的实现方式发生了巨大的变化.全新的UIPresentationController在实现视图控制器间的过渡动画效果和自适应设备 ...
- 开始使用 UIAlertController 吧
UIAlertView 与 UIActionSheet UIAlertView 样式 实现 - (void)showAlertView { self.alertView = [[UIAlertView ...
- UI第十四节——UIAlertController
- (void)viewDidLoad { [super viewDidLoad]; UIButton *alertBtn = [UIButton buttonWithType:U ...
- iOS 8.0后使用UIAlertController
iOS 8的新特性之一就是让接口更有适应性.更灵活,因此许多视图控制器的实现方式发生了巨大的变化.全新的UIPresentationController在实现视图控制器间的过渡动画效果和自适应设备尺寸 ...
随机推荐
- Mysql分库分表方案
Mysql分库分表方案 1.为什么要分表: 当一张表的数据达到几千万时,你查询一次所花的时间会变多,如果有联合查询的话,我想有可能会死在那儿了.分表的目的就在于此,减小数据库的负担,缩短查询时间. m ...
- LDAP与Samba
默认的Samba服务器支持本地系统用户(smbpasswd添加后)访问Samba资源,不支持OpenLDAP服务器账号访问Samba共享资源 目的:配置完后,OpenLDAP每新增一个用户,就自动支持 ...
- maven项目报:An error occurred while filtering resources
maven项目在problem中报: An error occurred while filtering resources 解决方法: 右键项目-maven-update project..
- Eclipse里面的一些常规设置
一.Eclipse里面的默认编码是GBK,但是Android开发的编码都是UTF-8,所以一定要修改自己的工程的编码,不要坑队友哦~ (1)选中当前的工程,点击右键,如图选择(这只是改变当前工程的编码 ...
- Eclipse 配置 pyDev(转)
转自:http://www.51testing.com/html/75/534775-859092.html 很久没有接触到Python了.以前都是在用2.7版本的,这次索性就安装3.2的了.一般的小 ...
- es6新特性(一)
关于es6,阮一峰的<ECMAScript 6入门> http://es6.ruanyifeng.com/写的非常详尽,可以经常看看,这里是对这本书进行一个缩略,可能有误,欢迎大家纠正.
- android:digits属性
android:digits属性限定输入的字符 可以用于密码输入等输入框 ex: 个人意见: 但是这样的话不符合要求的字符直接输入不上去,可能会给用户造成困惑,最好给出提示或用其他工具. 本文欢迎转载 ...
- php中ajax调用出错的问题
最近用一些php,客户的使用jquery ajax的时候经常报错 ajax报错内容:readyState:4 status:"OK",statusText:"OK&quo ...
- 2、C语言关键字-auto register static
文件限定符的作用: 1.auto : 局部变量,修饰的变量在栈中定义.动态内存,随着函数的结束,变量占用的内存空间也随之释放. 2.register : 寄存器变量,请求编译器将此变量存于cpu寄存器 ...
- Python.with.context-manager
Context Manager 1. Context Manager简介 "Context managers are a way of allocating and releasing so ...