警示框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 三次握手和四次挥手面试题」得到了很多读者的认可,在此特别感谢你们的认可,大家都暖暖的. 来了,今 ...
随机推荐
- Nexus:hardware type changed to No-Transceiver
如下是相关的案例: 1.N5K & Cat3750 较新版本的NX-OS在N5K上支持1G或10G,但N5K不支持auto-speed sensing 如下是故障信息的体现:3750和N5K之 ...
- Windows 10下一步一步创建 Scrapy框架的项目
此文是本人的学习笔记,网上搜索了很多资料,也走了一些弯路,记录下安装的过程,以便日后回顾 1.安装Anaconda3,安装时默认选项 2.装完Anaconda3后,打开系统变量在path路径下增加An ...
- P1567
最大子数组和问题,dp或者分治.. #include <bits/stdc++.h> #define rep(i, a, b) for(int i = a; i <= b; i++) ...
- 《MFC dialog中加入OpenGL窗体》
<MFC dialog中加入OpenGL窗体> 最近学习了如何在MFC对话框程序中加入OpenGL窗体的方法,在这里将自己的实现过程归纳一下. 步骤零: 加入PictureControl控 ...
- VMware虚拟磁盘修复
最近VMware虚拟机老是断掉提示无法完成同步,后来提示虚拟磁盘需要修复,经过一番查询,找到了相关检查与修复口令,先记录如下. vmware-vdiskmanager -R “PATH” 说明: PA ...
- leetcode菜鸡斗智斗勇系列(9)--- Range Sum of BST
1.原题: https://leetcode.com/problems/range-sum-of-bst/ Given the root node of a binary search tree, r ...
- ASP.NET(C#) Json序列化反序列化帮助类Jsonhelper
原文地址:https://ken.io/note/csharp-asp.net-jsonhelper using System; using System.Collections.Generic; u ...
- 走过的K8S坑
基本的docker命令: docker 镜像 打包成文件 sudo docker save -o 打包后的文件名 {镜像ID}或者{镜像标签} docker 改名: docker tag ff2816 ...
- Cisco AP-如何调整LAP信道
GUI方法: CLI的方法:根据对应的接口去调整信道,信道带宽,传输功率等信息吧.(Cisco Controller) >config slot 0 antenna Configures the ...
- Cisco AP-Flexconnect配置结果
一个部署Flexconnect AP(印度)注册到远端WLC(上海)的例子:1.连接AP的交换机接口的配置: nterface GigabitEthernet0/4switchport access ...