本文尽量图文并茂,并且提供对应的代码,确保看到这篇文章马上能够上手使用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的变动影响到这个知识了,我会及时更新的。

最新文章

  1. 小丁带你走进git世界一-git简单配置
  2. php生成网页桌面快捷方式
  3. Gradle命令行黑魔法
  4. C#制作艺术字
  5. Android Configuration change属性
  6. 149. Max Points on a Line
  7. CSS入门学习(转)
  8. Jsoup代码解读之一-概述
  9. apache-tomcat-7.0.70无法进入Manager管理App项目
  10. 第三弹:ZFNet
  11. c#算两个火星坐标的距离(高德or百度)
  12. 机器学习 - pycharm, pyspark, spark集成篇
  13. jsp注册页面的省份联动(网上copy别人的,然后自己弄了一下才知道怎么用)
  14. YYHS-NOIP2017SummerTraining0914-问题 A: 组合数问题
  15. SSM-SpringMVC-30:SpringMVC中InitBinder的骇客级优化
  16. 剑指offer——python【第14题】链表中倒数第k个节点
  17. 【HDOJ4109】【拓扑OR差分约束求关键路径】
  18. CSS-弹性布局-伪类选择器-复杂选择器
  19. 前端之javascript的数据类型1和BOM对象
  20. js便签笔记(7)——style、currentStyle、getComputedStyle区别介绍【转载】

热门文章

  1. Android学习03
  2. ASP.NET(C#) Json序列化反序列化帮助类Jsonhelper
  3. list随机生成数值
  4. python3.8+PySimpleGUI+进度条代码大全
  5. 吴裕雄--天生自然Numpy库学习笔记:NumPy Matplotlib
  6. 杭电 2013 猴子吃桃 递归解法&amp;循环解法
  7. python搭建后台服务
  8. 扒网站工具 HTTrack Website Copier
  9. 解决github访问慢和下载项目慢的问题
  10. 对于使用javaweb技术制作简单管理系统的学习