警示框UIAlertController的使用(看完马上会用!!)
本文尽量图文并茂,并且提供对应的代码,确保看到这篇文章马上能够上手使用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的使用(看完马上会用!!)的更多相关文章
- iOS UI-AlertView(警示框)和ActionSheet(选择框、操作表单)
#import "ViewController.h" @interface ViewController ()<UIAlertViewDelegate,UIActionShe ...
- APP的缓存文件到底应该存在哪?看完这篇文章你应该就自己清楚了
APP的缓存文件到底应该存在哪?看完这篇文章你应该就自己清楚了 彻底理解android中的内部存储与外部存储 存储在内部还是外部 所有的Android设备均有两个文件存储区域:"intern ...
- 看完这些,你就算得上既了解围棋又了解alphago了
首先,我们要祝贺小李下出第78手的“神之一手”,这一手堪称前无古人后无来者,尤其是结合了阿尔法狗自暴自弃的表现.小李说过他的失败并不是人类的失败,同样,小李的胜利也只是属于他一人的胜利. 然而人类在围 ...
- Windows PowerShell是啥?看完本文你就懂它了
这篇文章主要介绍了Windows PowerShell是啥?Windows PowerShell是什么?Windows PowerShell有哪些特性?Windows PowerShell有什么用?看 ...
- Android图表库MPAndroidChart(二)——线形图的方方面面,看完你会回来感谢我的
Android图表库MPAndroidChart(二)--线形图的方方面面,看完你会回来感谢我的 在学习本课程之前我建议先把我之前的博客看完,这样对整体的流程有一个大致的了解 Android图表库MP ...
- 图解Java线程的生命周期,看完再也不怕面试官问了
文章首发自个人微信公众号: 小哈学Java https://www.exception.site/java-concurrency/java-concurrency-thread-life-cycle ...
- 看完这篇还不会 GestureDetector 手势检测,我跪搓衣板!
引言 在 android 开发过程中,我们经常需要对一些手势,如:单击.双击.长按.滑动.缩放等,进行监测.这时也就引出了手势监测的概念,所谓的手势监测,说白了就是对于 GestureDetector ...
- 看完阮一峰的React教程后, 我写了一个TodoList
看完阮一峰的React教程后,就自己做了这个TodoList,自己慢慢琢磨效率差了点但是作为入门小练习还是不错的. 以下是效果图:我的源码:todolistUI:bootstrap 4 一.组件化 我 ...
- 【图解】你还在为 TCP 重传、滑动窗口、流量控制、拥塞控制发愁吗?看完图解就不愁了
每日一句英语学习,每天进步一点点: 前言 前一篇「硬不硬你说了算!近 40 张图解被问千百遍的 TCP 三次握手和四次挥手面试题」得到了很多读者的认可,在此特别感谢你们的认可,大家都暖暖的. 来了,今 ...
随机推荐
- 交换机出现err-disable的原因及解决方法
转:https://www.2cto.com/net/201303/198724.html 交换机出现err-disable的原因及解决方法 LOG示例: 21w6d: %ETHCNTR-3-LOOP ...
- leetcode 697
Given a non-empty array of non-negative integers nums, the degree of this array is defined as the ma ...
- 洛谷P1131 时态同步
题意: 给一个n点的树,每条边都有边权,问从根出发需要增加多少长度,使得最终的儿子到根的距离是一样的 思路: 上来一个思路wa了3次,看完题解之后,又一次豁然开朗…… orz #include< ...
- LeetCode练题——67. Add Binary
1.题目 67. Add Binary——easy Given two binary strings, return their sum (also a binary string). The inp ...
- 实现简单HttpServer案例
<html> <head> <title>第一个表单</title> </head> <body> <pre> me ...
- Install fail! Error: EBUSY: resource busy or locked, symlink
前些日子从github上下载了一个项目 然后放在桌面上 ,但是运行的时候 报了Install fail! Error: EBUSY: resource busy or locked, symlink ...
- XCOJ: 计算器
题目地址:http://xcacm.hfut.edu.cn/problem.php?id=1251 就和表达式求值有点像,但是和杭电的哪一题不一样的是中间没有空格,那么就意味着必须通过字符串处理的方式 ...
- Mac夜神模拟器99%无法正常使用
PS:部分因更新OS X导致的卡99%可以尝试更新VBOX来解决此问题. 下载VBOX地址:https://www.virtualbox.org/wiki/Downloads 选择对应 ...
- vue下canvas绘制矩形
起因:根据项目需求本人写了一个绘制矩形的组件.功能:在图片中绘制矩形,根据图片大小进行自适应展示,获取图片矩形坐标.思路:首先定义一个固定大小的DIV,DIV标签中有监测鼠标变化的四个事件moused ...
- 【Java 二维码】生成二维码
ZXingCodeEncodeUtils 生成及解析二维码项目 package utils; import java.awt.BasicStroke; import java.awt.Color; i ...