本文尽量图文并茂,并且提供对应的代码,确保看到这篇文章马上能够上手使用UIAlertController控件。~我要兑现我的务实宣言~


本文构思:

1、出具效果图,通过这种最直接方式了解该控件的展示效果看看是不是所需要的。

2、每种效果图对应的代码,绝对是拿出去直接可以显示出效果的。

3、根据苹果官方给出的UIAlertController的声明文件,总的再进行一次梳理知识。

    

    

为了描述,下面使用的是图1、图2、图3等等,来指代上面展示的7张图。接下来给出每个图对应的代码。

// 图一
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示"
message:@"确定取消活动?"
preferredStyle:UIAlertControllerStyleAlert];
// UIAlertActionStyleDefault,可以多个按钮为这个类型
UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) { }];
// UIAlertActionStyleCancel,只能有一个UIAlertAction实例对象为这种类型
// UIAlertActionStyleCancel类型的UIAlertAction按钮,永远都在最右边(最下面)的位置
// 默认UIAlertActionStyleCancel类型的UIAlertAction按钮,为preferredAction
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel
handler:^(UIAlertAction * action) { }];
// 添加3个以上的UIAlertAction按钮,就竖向排开。2个(含)以内就是水平排开
[alert addAction:sureAction];
[alert addAction:cancelAction];
// 只能使用模态的方式切入
[self.viewController presentViewController:alert animated:NO completion:nil];
// 图二
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示"
message:@"确定取消活动?"
preferredStyle:UIAlertControllerStyleAlert];
// UIAlertActionStyleDefault,可以多个按钮为这个类型
UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) { }];
// UIAlertActionStyleCancel,只能有一个UIAlertAction实例对象为这种类型
// UIAlertActionStyleCancel类型的UIAlertAction按钮,永远都在最右边(最下面)的位置
// 默认UIAlertActionStyleCancel类型的UIAlertAction按钮,为preferredAction
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel
handler:^(UIAlertAction * action) { }];
// UIAlertActionStyleDestructive颜色为红色,谨慎操作效果
UIAlertAction *maybeAction1 = [UIAlertAction actionWithTitle:@"随机1" style:UIAlertActionStyleDestructive
handler:^(UIAlertAction * action) { }];
// UIAlertActionStyleDefault,可以多个按钮为这个类型
UIAlertAction *maybeAction2 = [UIAlertAction actionWithTitle:@"随机2" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) { }];
// UIAlertActionStyleDefault,可以多个按钮为这个类型
UIAlertAction *maybeAction3 = [UIAlertAction actionWithTitle:@"随机3" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) { }];
// enabled = NO;按钮颜色变为灰色,并且不可点击
maybeAction3.enabled = NO;
// 添加3个以上的UIAlertAction按钮,就竖向排开。2个(含)以内就是水平排开
[alert addAction:sureAction];
[alert addAction:cancelAction];
[alert addAction:maybeAction1];
[alert addAction:maybeAction2];
[alert addAction:maybeAction3];
// 只能使用模态的方式切入
[self.viewController presentViewController:alert animated:NO completion:nil];
// 图三
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示"
message:@"确定取消活动?"
preferredStyle:UIAlertControllerStyleAlert];
// UIAlertActionStyleDefault,可以多个按钮为这个类型
UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) { }];
// UIAlertActionStyleCancel,只能有一个UIAlertAction实例对象为这种类型
// UIAlertActionStyleCancel类型的UIAlertAction按钮,永远都在最右边(最下面)的位置
// 默认UIAlertActionStyleCancel类型的UIAlertAction按钮,为preferredAction
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel
handler:^(UIAlertAction * action) { }];
// UIAlertActionStyleDestructive颜色为红色,谨慎操作效果
UIAlertAction *maybeAction1 = [UIAlertAction actionWithTitle:@"随机1" style:UIAlertActionStyleDestructive
handler:^(UIAlertAction * action) { }];
// UIAlertActionStyleDefault,可以多个按钮为这个类型
UIAlertAction *maybeAction2 = [UIAlertAction actionWithTitle:@"随机2" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) { }];
// UIAlertActionStyleDefault,可以多个按钮为这个类型
UIAlertAction *maybeAction3 = [UIAlertAction actionWithTitle:@"随机3" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) { }];
// enabled = NO;按钮颜色变为灰色,并且不可点击
maybeAction3.enabled = NO;
// 添加3个以上的UIAlertAction按钮,就竖向排开。2个(含)以内就是水平排开
[alert addAction:sureAction];
[alert addAction:cancelAction];
[alert addAction:maybeAction1];
[alert addAction:maybeAction2];
[alert addAction:maybeAction3];
// 默认是UIAlertActionStyleCancel类型的UIAlertAction按钮
// preferredAction后的UIAlertAction按钮,字体加粗,颜色等不变。只在UIAlertControllerStyleAlert时有效
// 但是依然是UIAlertActionStyleCancel类型的UIAlertAction按钮在最右边(最下面)的位置
alert.preferredAction = maybeAction2; // 只能使用模态的方式切入
[self.viewController presentViewController:alert animated:NO completion:nil];
// 图四
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示"
message:@"确定取消活动?"
preferredStyle:UIAlertControllerStyleAlert];
// UIAlertActionStyleDefault,可以多个按钮为这个类型
UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) { }];
// UIAlertActionStyleCancel,只能有一个UIAlertAction实例对象为这种类型
// UIAlertActionStyleCancel类型的UIAlertAction按钮,永远都在最右边(最下面)的位置
// 默认UIAlertActionStyleCancel类型的UIAlertAction按钮,为preferredAction
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel
handler:^(UIAlertAction * action) { }];
// 添加3个以上的UIAlertAction按钮,就竖向排开。2个(含)以内就是水平排开
[alert addAction:sureAction];
[alert addAction:cancelAction];
// reason: 'Text fields can only be added to an alert controller of style UIAlertControllerStyleAlert'==TextField只能在Alert是UIAlertControllerStyleAlert类型的时候才可以使用。
// 添加过多的TextField,Alert势必会显示不下。系统采取的策略是,先让Textfield区域往下延伸,使得UIAlertAction区域进行滚动。当把UIAlertAction区域拥挤到只能显示两个按钮,开始让Textfield区域也可以滚动。
[alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) { }];
// 可以通过textFields方法,取出添加进去的TextField,然后对UITextField对象进行操作
NSArray<UITextField *> *alertTextField = [alert textFields];
UITextField *firstTextField = [alertTextField firstObject];
firstTextField.text = @"firstTextField";
// 只能使用模态的方式切入
[self.viewController presentViewController:alert animated:NO completion:nil];
// 图五
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示"
message:@"确定取消活动?"
preferredStyle:UIAlertControllerStyleAlert];
// UIAlertActionStyleDefault,可以多个按钮为这个类型
UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) { }];
// UIAlertActionStyleCancel,只能有一个UIAlertAction实例对象为这种类型
// UIAlertActionStyleCancel类型的UIAlertAction按钮,永远都在最右边(最下面)的位置
// 默认UIAlertActionStyleCancel类型的UIAlertAction按钮,为preferredAction
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel
handler:^(UIAlertAction * action) { }];
// UIAlertActionStyleDestructive颜色为红色,谨慎操作效果
UIAlertAction *maybeAction1 = [UIAlertAction actionWithTitle:@"随机1" style:UIAlertActionStyleDestructive
handler:^(UIAlertAction * action) { }];
// UIAlertActionStyleDefault,可以多个按钮为这个类型
UIAlertAction *maybeAction2 = [UIAlertAction actionWithTitle:@"随机2" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) { }];
// UIAlertActionStyleDefault,可以多个按钮为这个类型
UIAlertAction *maybeAction3 = [UIAlertAction actionWithTitle:@"随机3" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) { }];
// enabled = NO;按钮颜色变为灰色,并且不可点击
maybeAction3.enabled = NO;
// 添加3个以上的UIAlertAction按钮,就竖向排开。2个(含)以内就是水平排开
[alert addAction:sureAction];
[alert addAction:cancelAction];
[alert addAction:maybeAction1];
[alert addAction:maybeAction2];
[alert addAction:maybeAction3];
// 默认是UIAlertActionStyleCancel类型的UIAlertAction按钮
// preferredAction后的UIAlertAction按钮,字体加粗,颜色等不变。只在UIAlertControllerStyleAlert时有效
// 但是依然是UIAlertActionStyleCancel类型的UIAlertAction按钮在最右边(最下面)的位置
alert.preferredAction = maybeAction2; // reason: 'Text fields can only be added to an alert controller of style UIAlertControllerStyleAlert'==TextField只能在Alert是UIAlertControllerStyleAlert类型的时候才可以使用。
// 添加过多的TextField,Alert势必会显示不下。系统采取的策略是,先让Textfield区域往下延伸,使得UIAlertAction区域进行滚动。当把UIAlertAction区域拥挤到只能显示两个按钮,开始让Textfield区域也可以滚动。
[alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) { }];
[alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
}];
[alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
}];
[alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
}];
[alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
}];
[alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
}];
[alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
}];
[alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
}];
[alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
}];
// 可以通过textFields方法,取出添加进去的TextField,然后对UITextField对象进行操作
NSArray<UITextField *> *alertTextField = [alert textFields];
UITextField *firstTextField = [alertTextField firstObject];
firstTextField.text = @"firstTextField"; // 只能使用模态的方式切入
[self.viewController presentViewController:alert animated:NO completion:nil];
// 图六
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示"
message:@"确定取消活动?"
preferredStyle:UIAlertControllerStyleActionSheet];
// UIAlertActionStyleDefault,可以多个按钮为这个类型
UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) { }];
// UIAlertActionStyleCancel,只能有一个UIAlertAction实例对象为这种类型
// UIAlertActionStyleCancel类型的UIAlertAction按钮,永远都在最右边(最下面)的位置
// 默认UIAlertActionStyleCancel类型的UIAlertAction按钮,为preferredAction
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel
handler:^(UIAlertAction * action) { }]; // 添加3个以上的UIAlertAction按钮,就竖向排开。2个(含)以内就是水平排开
[alert addAction:sureAction];
[alert addAction:cancelAction]; // 只能使用模态的方式切入
[self.viewController presentViewController:alert animated:NO completion:nil];
// 图七
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示"
message:@"确定取消活动?"
preferredStyle:UIAlertControllerStyleActionSheet];
// UIAlertActionStyleDefault,可以多个按钮为这个类型
UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) { }];
// UIAlertActionStyleCancel,只能有一个UIAlertAction实例对象为这种类型
// UIAlertActionStyleCancel类型的UIAlertAction按钮,永远都在最右边(最下面)的位置
// 默认UIAlertActionStyleCancel类型的UIAlertAction按钮,为preferredAction
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel
handler:^(UIAlertAction * action) { }];
// UIAlertActionStyleDestructive颜色为红色,谨慎操作效果
UIAlertAction *maybeAction1 = [UIAlertAction actionWithTitle:@"随机1" style:UIAlertActionStyleDestructive
handler:^(UIAlertAction * action) { }];
// UIAlertActionStyleDefault,可以多个按钮为这个类型
UIAlertAction *maybeAction2 = [UIAlertAction actionWithTitle:@"随机2" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) { }];
// UIAlertActionStyleDefault,可以多个按钮为这个类型
UIAlertAction *maybeAction3 = [UIAlertAction actionWithTitle:@"随机3" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) { }];
// enabled = NO;按钮颜色变为灰色,并且不可点击
maybeAction3.enabled = NO;
// 添加3个以上的UIAlertAction按钮,就竖向排开。2个(含)以内就是水平排开
[alert addAction:sureAction];
[alert addAction:cancelAction];
[alert addAction:maybeAction1];
[alert addAction:maybeAction2];
[alert addAction:maybeAction3];
// 默认是UIAlertActionStyleCancel类型的UIAlertAction按钮
// preferredAction后的UIAlertAction按钮,字体加粗,颜色等不变。只在UIAlertControllerStyleAlert时有效
// 但是依然是UIAlertActionStyleCancel类型的UIAlertAction按钮在最右边(最下面)的位置
alert.preferredAction = maybeAction2; // 只能使用模态的方式切入
[self.viewController presentViewController:alert animated:NO completion:nil];

梳理知识方面的话,我觉得如果对着上面的效果图,再加上把代码运行在Xcode上的话,关于这个控件的时候我相信应该是已经掌握了的。

~本文到此结束,如果后面苹果SDK的变动影响到这个知识了,我会及时更新的。

警示框UIAlertController的使用(看完马上会用!!)的更多相关文章

  1. iOS UI-AlertView(警示框)和ActionSheet(选择框、操作表单)

    #import "ViewController.h" @interface ViewController ()<UIAlertViewDelegate,UIActionShe ...

  2. APP的缓存文件到底应该存在哪?看完这篇文章你应该就自己清楚了

    APP的缓存文件到底应该存在哪?看完这篇文章你应该就自己清楚了 彻底理解android中的内部存储与外部存储 存储在内部还是外部 所有的Android设备均有两个文件存储区域:"intern ...

  3. 看完这些,你就算得上既了解围棋又了解alphago了

    首先,我们要祝贺小李下出第78手的“神之一手”,这一手堪称前无古人后无来者,尤其是结合了阿尔法狗自暴自弃的表现.小李说过他的失败并不是人类的失败,同样,小李的胜利也只是属于他一人的胜利. 然而人类在围 ...

  4. Windows PowerShell是啥?看完本文你就懂它了

    这篇文章主要介绍了Windows PowerShell是啥?Windows PowerShell是什么?Windows PowerShell有哪些特性?Windows PowerShell有什么用?看 ...

  5. Android图表库MPAndroidChart(二)——线形图的方方面面,看完你会回来感谢我的

    Android图表库MPAndroidChart(二)--线形图的方方面面,看完你会回来感谢我的 在学习本课程之前我建议先把我之前的博客看完,这样对整体的流程有一个大致的了解 Android图表库MP ...

  6. 图解Java线程的生命周期,看完再也不怕面试官问了

    文章首发自个人微信公众号: 小哈学Java https://www.exception.site/java-concurrency/java-concurrency-thread-life-cycle ...

  7. 看完这篇还不会 GestureDetector 手势检测,我跪搓衣板!

    引言 在 android 开发过程中,我们经常需要对一些手势,如:单击.双击.长按.滑动.缩放等,进行监测.这时也就引出了手势监测的概念,所谓的手势监测,说白了就是对于 GestureDetector ...

  8. 看完阮一峰的React教程后, 我写了一个TodoList

    看完阮一峰的React教程后,就自己做了这个TodoList,自己慢慢琢磨效率差了点但是作为入门小练习还是不错的. 以下是效果图:我的源码:todolistUI:bootstrap 4 一.组件化 我 ...

  9. 【图解】你还在为 TCP 重传、滑动窗口、流量控制、拥塞控制发愁吗?看完图解就不愁了

    每日一句英语学习,每天进步一点点: 前言 前一篇「硬不硬你说了算!近 40 张图解被问千百遍的 TCP 三次握手和四次挥手面试题」得到了很多读者的认可,在此特别感谢你们的认可,大家都暖暖的. 来了,今 ...

随机推荐

  1. 【原】docker-compose 管理docker的多容器配置

    docker-compose管理docker的多容器配置,实现docker的自动化. version: '3.4' x-defaults: &defaults restart: unless- ...

  2. MS17_010漏洞攻击Windows7

    攻击主机系统:Kali Linux 2018 目标主机系统:Windows7 x64 1.攻击主机启动Metasploit: msfconsole 2.查找MS17_010漏洞相关的信息: searc ...

  3. 喵星之旅-狂奔的兔子-redis使用

    一.命令行使用 redis大概有200多命令,这里只是入门级别,列举了一些非常常见的内容,如果这些会了就可以开启redis进一步学习了. 1.登录数据库 我们需要知道ip地址.端口号.密码(如果有). ...

  4. MDC 输出线程信息帮助定位问题

    log4j中的%x ---NDC,%X---MDC 即%x NDC.clear();NDC.push(this.toString());%X{first} %X{last}MDC.put(" ...

  5. Java 中序列化与反序列化引发的思考?

    java 中序列化指从对象转变为 二进制流的过程中需要进行序列化,而反序列化指二进制流转换为java 对象.那么有的时候java 存储到数据库不需要序列化, 而计算机系统本质存储的就是二进制文件,数据 ...

  6. FTP文件传输服务!

    一.FTP  连接及传输模式 1.控制连接:TCP 21,用于发送 FTP 命令信息2.数据连接:TCP 20,用于上传.下载数据3.数据连接的建立类型: (1)主动模式:服务器主动发起数据连接 (2 ...

  7. mysql服务删除成功,依然存在

    重启电脑, 这可能是缓存, mysqld remove 删除后,偶尔,mysql服务依然存在,重启电脑,解决,

  8. 2019最新整理JAVA面试题附答案

    本人免费整理了Java高级资料,涵盖了Java.Redis.MongoDB.MySQL.Zookeeper.Spring Cloud.Dubbo高并发分布式等教程,一共30G,需要自己领取.传送门:h ...

  9. 如何查看NXP产品的供货计划?

    大的半导体厂商一般会提供每个产品的生命周期计划,NXP的工业级IC一般供货10年,汽车级是15年,具体的时间可以在官网查询得到. 首先,打开NXP官网链接 产品长期供货计划,可以看到以下页面 接着,筛 ...

  10. 单链表 C++ 实现 - 含虚拟头节点

    本文例程下载链接:ListDemo 链表 vs 数组 链表和数组的最大区别在于链表不支持随机访问,不能像数组那样对任意一个(索引)位置的元素进行访问,而需要从头节点开始,一个一个往后访问直到查找到目标 ...