在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. eclipse黑色主题

    Lennon http://www.cnblogs.com/csulennon/p/4231405.html

  2. Android下载图片/调用系统相机拍照、显示并保存到本地

    package com.example.testhttpget; import java.io.BufferedReader; import java.io.FileNotFoundException ...

  3. for xml 字符串拼接

    SELECT TOP 10 B.user_gid,LEFT(StuList,LEN(StuList)-1) as hobby FROM (SELECT user_gid,( SELECT CAST(a ...

  4. Android 记录和恢复ListView滚动的位置的三种方法

    本文主要介绍记录和恢复listView滚动位置的3种方法(1)记录listView滚动到的位置的坐标(推荐)(2)记录listView显示在屏幕上的第一个item的位置(3)通知适配器数据改变. 有时 ...

  5. thinkpad e450 win7黑苹果macos 10.10.5(网/显/声卡驱动)安装成功

    首先上图: 过程: 1.使用变色龙安装macos 10.10.5懒人版黑苹果 2.使用Haswell破解内核替换,成功进入系统 2.5.使用Hackintosh Vietnam Tool 1.9.6以 ...

  6. redirect和forward

    1.重定向 <mvc:view-controller path="/" view-name="redirect:/admin/index"/>即如果 ...

  7. UIView中间透明周围半透明(四种方法)

    方法一 #import "DrawView.h" @implementation DrawView - (instancetype)initWithFrame:(CGRect)fr ...

  8. SikuliLibrary 库关键字注释

    在  https://github.com/rainmanwy/robotframework-SikuliLibrary 看到rainmanwy 整理的SikuliLibrary库,非常适合工作需要, ...

  9. XE3随笔21:系统默认语言与系统支持的语言列表

    unit Unit1; interface uses   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, For ...

  10. rdlc报表大小设置

    参考:http://stackoverflow.com/questions/427730/how-to-limit-rdlc-report-for-one-page-in-a-pdf 主要设置为:报表 ...