在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的使用的更多相关文章

  1. UIAlertController

    楼主在整理项目的警告,于是乎你懂的. 然后自己整理了一下以后方便自己忘了之后能及时找到它 关于UIAlertController .h文件的解析 /** 关于UIAlertController的解析 ...

  2. iOS UIAlertController跟AlertView用法一样 && otherButtonTitles:(nullable NSString *)otherButtonTitles, ... 写法

    今天写弹出框UIAlertController,用alertView习惯了,所以封装了一下,跟alertView用法一样,不说了,直接上代码: 先来了解一下otherButtonTitles:(nul ...

  3. UIAlertController使用

    // 将UIAlertController模态出来 相当于UIAlertView show 的方法// 初始化一个一个UIAlertController    // 参数preferredStyle: ...

  4. IOS UIAlertController 使用方法

    在很多种语言中,alert都表示弹窗操作,弹窗功能非常有用,不仅可以用于正式的app功能中,也可以在调试中使用.在OC中,UIAlertController类用来控制弹窗操作.在IOS 8.0之前, ...

  5. UI控件(UIAlertController)

    @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; UIButton *_button = [UIBut ...

  6. UIAlertController 部分用法及属性

    //创建UIAlertController:初始化UIAlertController 需要使用alertControllerWithTitle UIAlertController *alertCont ...

  7. iOS--UIAlertView与UIAlertController和UIAlertAction之间的事儿

      iOS 8的新特性之一就是让接口更有适应性.更灵活,因此许多视图控制器的实现方式发生了巨大的变化.全新的UIPresentationController在实现视图控制器间的过渡动画效果和自适应设备 ...

  8. 开始使用 UIAlertController 吧

    UIAlertView 与 UIActionSheet UIAlertView 样式 实现 - (void)showAlertView { self.alertView = [[UIAlertView ...

  9. UI第十四节——UIAlertController

    - (void)viewDidLoad {    [super viewDidLoad];        UIButton *alertBtn = [UIButton buttonWithType:U ...

  10. iOS 8.0后使用UIAlertController

    iOS 8的新特性之一就是让接口更有适应性.更灵活,因此许多视图控制器的实现方式发生了巨大的变化.全新的UIPresentationController在实现视图控制器间的过渡动画效果和自适应设备尺寸 ...

随机推荐

  1. Redis学习笔记-初级

    1.Redis简介 Redis是一个高效缓存内存数据库,开源.免费.key-value格式 相比于其他key-value格式的缓存产品,特点/优势在于: 支持持久化,可以将内存中的数据保存在磁盘中,重 ...

  2. Ubuntu 安装 mysql 并修改数据库目录

    . . . . . 今天折腾了一下午的时间,恢复了无数次虚拟机快照,终于在 Ubuntu 上把 mysql 安装好了. mysql 是从官网下载的:mysql-server_5.7.16-1ubunt ...

  3. 定义/修改列时 NULL

    mysql的文档说明: column_definition:     col_name type [NOT NULL | NULL] [DEFAULT default_value]         [ ...

  4. svn:ignore eclipse开发一般忽略文件

    target.project.classpath.settings

  5. 常用的PHP框架

    ThinkPHP  http://www.thinkphp.cn Yii            http://www.yiichina.com laravel      https://laravel ...

  6. 深入浅出Symfony2 - 结合MongoDB开发LBS应用

    简介 随着近几年各类移动终端的迅速普及,基于地理位置的服务(LBS)和相关应用也越来越多,而支撑这些应用的最基础技术之一,就是基于地理位置信息的处理.我所在的项目也正从事相关系统的开发,我们使用的是S ...

  7. Win7(32bit)下Qt5.5.0和OpenCV2.4.9环境的搭建

    之前一直基于Windows平台(Win 7 32bit)使用VS2010做OpenCV的开发,现在开始学习在Windows 下使用Qt做OpenCV的开发.本文主要讲述Windows平台下(Win 7 ...

  8. OpenSSL Heartbleed原因小结

    User发送心跳报文给Server,Server复制心跳报文的内容回应User. memcpy(bp, p1, payload); Server拷贝心跳报文的内容给Client时,如果拷贝的字节数目超 ...

  9. oracle数据库升级记(记一次10.2.0.3版本升级到11.2.0.1版本的过程)

    操作系统:windows xp 已有数据库版本:10.2.0.3 升级目标版本:11.2.0.1 步骤大纲: 在源操作系统(安装有10.2.0.3数据库的操作系统)上安装11.2.0.1数据库软件,然 ...

  10. AIX 查看当前目录内最大的10个目录

       du  | sort -r n| head -n10 du  查看目录大小: srot  排序,-r 选择倒序,-n选项 看作数值排序(否则将作为字符排序): herd   -n10  查看开通 ...