本文尽量图文并茂,并且提供对应的代码,确保看到这篇文章马上能够上手使用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. 超长干货丨Kubernetes网络快速入门完全指南

    Kubernetes网络一直是一个非常复杂的主题.本文将介绍Kubernetes实际如何创建网络以及如何为Kubernetes集群设置网络. 本文不包括如何设置Kubernetes集群.这篇文章中的所 ...

  2. stl_string复习

    #include <iostream>#include <string>#include <algorithm>using namespace std; void ...

  3. 学好Linux必备知识

    鸟哥的私房菜中提到学好Linux必备的几种技能: 1.  计算器概论不硬件相关知识: 因为既然想要走Linux这门路,信息相关癿基础技能也丌能没有啊! 所以先理觋一下基础癿硬件知识,丌用一定要全懂啦! ...

  4. Linux 的三种软件安装包介绍

    通过RPM软件包来安装 说起RPM(RedHat Package Management)标准的软件包,大家可能都会想起大名鼎鼎的REDHAT公司,正是RPM软件包发行方式的出现,使Linux中的应用软 ...

  5. Bugku - CTF加密篇之聪明的小羊(一只小羊翻过了2个栅栏)

    聪明的小羊 一只小羊翻过了2个栅栏 KYsd3js2E{a2jda}  

  6. Introducing .NET 5

    Today, we’re announcing that the next release after .NET Core 3.0 will be .NET 5. This will be the n ...

  7. 获取class对象的三种方法以及通过Class对象获取某个类中变量,方法,访问成员

    public class ReflexAndClass { public static void main(String[] args) throws Exception { /** * 获取Clas ...

  8. CF908D 【New Year and Arbitrary Arrangement】

    蒟蒻渣渣禹小心翼翼发布题解.... 这道题,嗯,期望,dp,好,我们有思路了.... however, 主要问题在于字符串无限延伸,so,我们需要考虑记录前缀的关键量来为DP设置终止状态. 我们不妨设 ...

  9. 树莓派4B踩坑指南 - (6)安装常用软件及相关设置

    安装软件 安装LibreOffice中文包 sudo apt-get install libreoffice-l10n-zh-cn sudo reboot 安装codeblocks并汉化: sudo ...

  10. SearchRequest用于与搜索文档、聚合、定制查询有关的任何操作

    SearchRequest用于与搜索文档.聚合.定制查询有关的任何操作,还提供了在查询结果的基于上,对于匹配的关键词进行突出显示的方法. 1,首先创建搜索请求对象:SearchRequest sear ...