UIAlertController用法大全
来源:互联网 发布:淘宝店铺怎么设置打折 编辑:程序博客网 时间:2024/05/19 01:08
iOS 8的新特性之一就是让接口更有适应性、更灵活,因此许多视图控制器的实现方式发生了巨大的变化。全新的UIPresentationController在实现视图控制器间的过渡动画效果和自适应设备尺寸变化效果(比如说旋转)中发挥了重要的作用,它有效地节省了程序员们的工作量.UIAlertView和UIActionSheet不赞成在iOS 8中使用, 取而代之的是UIAlertController, 这个类以一种模块化替换的方式来代替这两货的功能和作用。是使用对话框(alert)还是使用上拉菜单(action sheet),就取决于在创建控制器时,您是如何设置首选样式的。
import “MainViewController.h”
@interface MainViewController ()
@end
@implementation MainViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
pragma mark - 对话框样式
/* 创建UIAlertController对象 */
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"My Alert" message:@"WELCOME" preferredStyle:UIAlertControllerStyleAlert];
pragma mark - 配置提示
/* 设置提示标题 */
alertController.title = @"My Alert";
/* 设置提示信息 */
alertController.message = @"WELCOME";
/* 设置提示风格 */
/*typedef enum UIAlertControllerStyle: NSInteger { UIAlertControllerStyleActionSheet = 0, 上拉菜单样式 UIAlertControllerStyleAlert 对话框样式} UIAlertControllerStyle;*/
/* 创建UIAlertAction对象, 添加到控制器上 */
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {}];UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancle" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {}];
pragma mark - 配置动作属性
/* 设置动作按钮是否能够被使用, 默认为YES*/
okAction.enabled = YES;
/* 设置动作按钮风格 */
/* typedef enum UIAlertActionStyle: NSInteger { UIAlertActionStyleDefault = 0, 默认风格 UIAlertActionStyleCancel, 取消操作风格, 点击不会更改任何事情 要注意,取消按钮是唯一的,如果您添加了第二个取消按钮,那么你运行时会异常 UIAlertActionStyleDestructive 消除风格, 点击可能会改变或删除数据 } UIAlertActionStyle; */
pragma mark - 配置文本输入框
/* 在提示上加一个文本输入框, 只有在对话框样式下使用 */
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) { textField.placeholder = @"账号"; /* 得到通知并自动更新 */ [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(alertTextFieldDidChange:) name:UITextFieldTextDidChangeNotification object:textField];}];[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) { textField.placeholder = @"密码"; textField.secureTextEntry = YES;}];
pragma mark - 配置用户点击动作
/* 添加动作 */
[alertController addAction:cancelAction];[alertController addAction:okAction];
/* 冻结OK按钮*/
okAction.enabled = NO;
/* 显示对话框视图控制器 */
[self presentViewController:alertController animated:YES completion:^{}];
pragma mark - 上拉菜单样式
/* UIAlertControllerStyle 选择上拉菜单模式 */
UIAlertController *alertController2 = [UIAlertController alertControllerWithTitle:@"保存或删除数据" message:@"数据删除不可恢复" preferredStyle:UIAlertControllerStyleActionSheet];
/* 创建UIAlertAction对象, 添加到控制器上 */
/* UIAlertController在使用弹出框的时候自动移除了取消按钮。用户通过点击弹出框的外围部分来实现取消操作,因此取消按钮便不再必需。 */
/* 如果上拉菜单中有“取消”按钮的话,那么它永远都会出现在菜单的底部,不管添加的次序是如何(就是这么任性) */
UIAlertAction *cancelAction2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {}];
/* 《iOS 用户界面指南》要求所有的“毁坏”样式按钮都必须排名第一 */
UIAlertAction *deleteAction = [UIAlertAction actionWithTitle:@"删除" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {}];UIAlertAction *archiveAction = [UIAlertAction actionWithTitle:@"保存" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {}];[alertController2 addAction:cancelAction2];[alertController2 addAction:deleteAction];[alertController2 addAction:archiveAction];
pragma mark - 上拉菜单样式不允许添加文本输入框, 否则会程序异常
/* 显示上拉菜单视图控制器 注意:当有对话框样式的提示时, 上拉菜单样式将不会被显示, 请先将显示对话框视图控制器注掉 */
[self presentViewController:alertController2 animated:YES completion:^{}];
pragma mark ** 别激动得太早,我们现在还有一个很严重的问题,这个问题隐藏得比较深。当我们使用iPad或其他常规宽度的设备时,就会运行异常, 需要设置其弹出框大小
/* UIPopoverPresentationController类同样是iOS8中新出现的类, 用来替换UIPopoverController的。这个时候上拉菜单是以一个固定在源按钮上的弹出框的形式显示的。 */
UIPopoverPresentationController *popover = alertController2.popoverPresentationController;if (popover) { popover.sourceView = self.view; popover.sourceRect = self.view.bounds; popover.permittedArrowDirections = UIPopoverArrowDirectionAny;}
}
/* 激活OK按钮前检查”账号”文本框的内容, 如果文本长度大于2, 则激活 */
- (void)alertTextFieldDidChange:(NSNotification *)notification
{
UIAlertController alertController = (UIAlertController )self.presentedViewController;
if (alertController) {
UITextField *login = alertController.textFields.firstObject;
UIAlertAction *okAction = alertController.actions.lastObject;
okAction.enabled = login.text.length > 2;
}
}
- UIAlertController用法大全
- UIAlertController的用法
- UIAlertController的用法示例
- iOS - UIAlertController相关用法
- UIAlertController的用法
- UIAlertController详解及用法
- UIAlertController的用法
- UIAlertController 用法简介
- UIAlertController 基本用法一
- UIAlertController的用法
- UIAlertController的用法
- UIAlertController的用法
- UIAlertController的用法
- 关于UIAlertController 的一些用法
- ios9新增 UIAlertController 简单用法
- UIAlertController中TextField的用法
- UIAlertController在Swift中的用法
- UIAlertController
- 谷歌技术"三宝"之谷歌文件系统
- ThreadLocal用法和实现原理
- Google后Hadoop时代的新“三驾马车”
- 华为OJ题目:24点运算
- 配置python环境
- UIAlertController用法大全
- nc命令
- 跨浏览器添加事件处理程序
- 杭电 2094 产生冠军(拓扑排序)
- JavaScript高级程序设计 第二章 --- 在HTML中使用JavaScript
- 【Ajax技术】使用XHR对象发送和接受数据
- LeetCode: Implement pow(x, n).
- Android SwipeBackLayout返回黑屏的处理
- Anroid 布局管理器