开始使用 UIAlertController 吧
UIAlertView 与 UIActionSheet
- UIAlertView
样式
实现
- (void)showAlertView {
self.alertView = [[UIAlertView alloc] initWithTitle:@"确定操作吗?" message:@"确定可能会有灾难哦!" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
self.alertView.delegate = self;
[self.alertView show];
}
注意
- 其“确定”按钮的颜色与“取消”按钮的外观一样(没有显示红色,即 normal)
- UIActionSheet
样式
实现
- (void)showActionSheet {
self.actionSheet = [[UIActionSheet alloc] initWithTitle:@"确定操作吗?" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定" otherButtonTitles:nil, nil];
[self.actionSheet showInView:self.view];
}
注意
- 其“确定”按钮的颜色与“取消”按钮的外观不一样(显示红色,即 destructive)
UIAlertController
概述
UIAlertController出现的原因,我想就不必多说了。来看看苹果官方的介绍吧!
A UIAlertController object displays an alert message to the user. This class replaces the UIActionSheet and UIAlertView classes for displaying alerts. After configuring the alert controller with the actions and style you want, present it using the presentViewController:animated:completion: method UIAlertController 实例是用来向用户警告信息的。该类旨在替代 UIActionSheet 和 UIAlertView。若你按照自己的需求配置了 UIAlertController 的 actions 和 style,就使用 presentViewController:animated:completion: 方法来显示它吧
UIAlertController 的使用步骤
- 初始化 UIAlertController, 并设置标题,副标题,alert的样式(alert 或 actionSheet)
- 添加事件
- 使用 UIAlertAction 定义每一个事件,与事件相关的 title、style、action
- style
- UIAlertActionStyleDefault
- UIAlertActionStyleCancel
- UIAlertActionStyleDestructive
- action
- 使用 block 代替了原来的 代理模式
- 显示 alert
UIAlertController 的简单使用
显示 alertView
初始化 UIAlertController
self.alertController = [UIAlertController alertControllerWithTitle:@"确定操作吗?" message:@"确定可能会有灾难哦!" preferredStyle:UIAlertControllerStyleAlert];
添加事件(
事件的添加顺序,会影响按钮的显示顺序
)UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//TODO:
}];
[self.alertController addAction:cancelAction];
UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
//TODO:
}];
[self.alertController addAction:confirmAction];
显示 alert
[self presentViewController:self.alertController animated:YES completion:^{
// TODO
}];
显示 actionSheet
- 在此就不再贴代码了,把 上述代码的 UIAlertControllerStyleAlert 改成 UIAlertControllerStyleActionSheet 试试吧
UIAlertController 中的 textField
- 注意
只能向 alert 类型的 UIAlertController 中添加 textField
向 actionSheet 类型的 UIAlertController 中添加 textField,会报运行时错误
Text fields can only be added to an alert controller of style UIAlertControllerStyleAlert
- 使用 UIAlertController 实现登录界面(
demo
)效果
具体实现
- 使用 CocoaPods 集成 MBProgressHUD 框架(CocoaPods的安装和使用那些事(Xcode 7.2,iOS 9.2,Swift))
编辑 podfile 文件,如下:
为了使用的方便,通常会为 MBProgressHUD 添加分类,在此只添加 showMessage 方法,如下
+ (void)showMessage:(NSString *)message {
// hud 显示的 view
UIView *contentView = [[UIApplication sharedApplication].windows lastObject];
// hud
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:contentView animated:YES];
// hud 显示的信息
hud.detailsLabelText = message;
// 当 hud 隐藏时是否从父控件中移除
hud.removeFromSuperViewOnHide = YES;
// hub 显示的时间
[hud hide:YES afterDelay:1.5f];
}
- 设置 UIAlertController
初始化 UIAlertController
self.alertController = [UIAlertController alertControllerWithTitle:@"登录" message:nil preferredStyle:UIAlertControllerStyleAlert];
添加 textField(
textField 与 action 的添加顺序,不影响其显示顺序
)[self.alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = @"账户";
}];
[self.alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = @"密码";
}];
添加事件
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
// TODO:
}];
[self.alertController addAction:cancelAction];
UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
// get the account and password
UITextField *accountTextField = self.alertController.textFields[0];
UITextField *passwordTextField = self.alertController.textFields[1];
NSString *message = [NSString stringWithFormat:@"账户:%@\n密码:%@", accountTextField.text, passwordTextField.text];
// 显示 MBProgressHUD(需要在主线程中显示)
dispatch_async(dispatch_get_main_queue(), ^{
[MBProgressHUD showMessage:message];
});
}];
[self.alertController addAction:confirmAction];
显示 alert
[self presentViewController:self.alertController animated:YES completion:^{
// TODO
}];
- 使用 CocoaPods 集成 MBProgressHUD 框架(CocoaPods的安装和使用那些事(Xcode 7.2,iOS 9.2,Swift))
开始使用 UIAlertController 吧的更多相关文章
- UIAlertController
楼主在整理项目的警告,于是乎你懂的. 然后自己整理了一下以后方便自己忘了之后能及时找到它 关于UIAlertController .h文件的解析 /** 关于UIAlertController的解析 ...
- iOS UIAlertController跟AlertView用法一样 && otherButtonTitles:(nullable NSString *)otherButtonTitles, ... 写法
今天写弹出框UIAlertController,用alertView习惯了,所以封装了一下,跟alertView用法一样,不说了,直接上代码: 先来了解一下otherButtonTitles:(nul ...
- UIAlertController使用
// 将UIAlertController模态出来 相当于UIAlertView show 的方法// 初始化一个一个UIAlertController // 参数preferredStyle: ...
- IOS UIAlertController 使用方法
在很多种语言中,alert都表示弹窗操作,弹窗功能非常有用,不仅可以用于正式的app功能中,也可以在调试中使用.在OC中,UIAlertController类用来控制弹窗操作.在IOS 8.0之前, ...
- UI控件(UIAlertController)
@implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; UIButton *_button = [UIBut ...
- UIAlertController 部分用法及属性
//创建UIAlertController:初始化UIAlertController 需要使用alertControllerWithTitle UIAlertController *alertCont ...
- iOS--UIAlertView与UIAlertController和UIAlertAction之间的事儿
iOS 8的新特性之一就是让接口更有适应性.更灵活,因此许多视图控制器的实现方式发生了巨大的变化.全新的UIPresentationController在实现视图控制器间的过渡动画效果和自适应设备 ...
- UI第十四节——UIAlertController
- (void)viewDidLoad { [super viewDidLoad]; UIButton *alertBtn = [UIButton buttonWithType:U ...
- iOS 8.0后使用UIAlertController
iOS 8的新特性之一就是让接口更有适应性.更灵活,因此许多视图控制器的实现方式发生了巨大的变化.全新的UIPresentationController在实现视图控制器间的过渡动画效果和自适应设备尺寸 ...
随机推荐
- Chrome开发工具之Console
Chrome开发工具-Console 看了别人的博客,才发现在百度主页用开发工具“Console”可以看到百度的招聘信息 前端调试工具可以按F12打开,谷歌的开发工具中的Console面板可以查看错误 ...
- 你所不知道的15个Axure使用技巧
你有用原型开发工具吗?如果有,那你用的是Axure还是别的? 从以前就喜欢使用Axure,主要是觉得它能清楚的表达设计的思路,还有交互的真实再现,能让看的人一目了然,昨天看了这篇博文,便更加确定Axu ...
- 数据库操作提示:Specified key was too long; max key length is 767 bytes
操作重现: 法1:新建连接——>新建数据库——>右键数据库导入脚本——>提示:Specified key was too long; max key length is 767 by ...
- ECMAScript 6 开篇准备
1前言 该系列文章均为学习阮一峰老师<ECMAScript 6 入门>一书的学习笔记.原著:http://es6.ruanyifeng.com/ 各大浏览器的最新版本,对ES6的支持可以查 ...
- Oracle数据库的SQL分页模板
在系统开发过程中,需要对数据进行查询,大部分情况下从数据库中查询的数据量比较大,在系统页面无法全部显示,而且查询全部的数据会影响系统的反应速度,需要对所查询的数据进行分页的查询操作,以此减轻系统的压力 ...
- SQL Server 存储过程中处理多个查询条件的几种常见写法分析,我们该用那种写法
本文出处: http://www.cnblogs.com/wy123/p/5958047.html 最近发现还有不少做开发的小伙伴,在写存储过程的时候,在参考已有的不同的写法时,往往很迷茫,不知道各种 ...
- MVC中实现加载更多
需要实现的功能: 数据太多想初次加载部分数据,在底部加上“加载更多”按钮 点击后加载第二页数据(从数据库只取指定页数据)后接在已有数据后面(类似于android中的下拉加载更多) 每次加载时显示“正在 ...
- jQuery-1.9.1源码分析系列(十五) 动画处理——外篇
a.动画兼容Tween.propHooks Tween.propHooks提供特殊情况下设置.获取css特征值的方法,结构如下 Tween.propHooks = { _default: { get: ...
- Struts2 源码分析——拦截器的机制
本章简言 上一章讲到关于action代理类的工作.即是如何去找对应的action配置信息,并执行action类的实例.而这一章笔者将讲到在执行action需要用到的拦截器.为什么要讲拦截器呢?可以这样 ...
- Net设计模式实例之抽象工厂模式(Abstract Factory Pattern)
一.抽象工厂模式简介(Bref Introduction) 抽象工厂模式(Abstract Factory Pattern),提供一个创建一系列相关或者相互依赖对象的接口,而无需制定他们的具体类.优点 ...