IOS8 : UIAlertController
UIAlertController 和 UIAlertAction 用法:
1. 最简单的提醒视图:
这里我们实现一个最简单的提醒视图,包含1个标题,1行信息,1个按键,按下按键后,什么都不发生:
- - (IBAction)doAlert:(id)sender {
- // 准备初始化配置参数
- NSString *title = @"Alert Button Selected";
- NSString *message = @"I need your attention NOW!";
- NSString *okButtonTitle = @"OK";
- // 初始化
- UIAlertController *alertDialog = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
- // 创建操作
- UIAlertAction *okAction = [UIAlertAction actionWithTitle:okButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
- // 操作具体内容
- // Nothing to do.
- }];
- // 添加操作
- [alertDialog addAction:okAction];
- // 呈现警告视图
- [self presentViewController:alertDialog animated:YES completion:nil];
- }
进入程序后,点击“Alert Me!”按钮可触发这个提醒框,如图所示:
- - (IBAction)doMultiButtonAlert:(id)sender {
- // 准备初始化配置参数
- NSString *title = @"Alert Button Selected";
- NSString *message = @"I need your attention NOW!";
- NSString *okButtonTitle = @"OK";
- NSString *neverButtonTitle = @"Never";
- NSString *laterButtonTitle = @"Maybe Later";
- // 初始化
- UIAlertController *alertDialog = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
- // 分别3个创建操作
- UIAlertAction *laterAction = [UIAlertAction actionWithTitle:laterButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
- // 普通按键
- self.userOutput.text = @"Clicked 'Maybe Later'";
- }];
- UIAlertAction *neverAction = [UIAlertAction actionWithTitle:neverButtonTitle style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
- // 红色按键
- self.userOutput.text = @"Clicked 'Never'";
- }];
- UIAlertAction *okAction = [UIAlertAction actionWithTitle:okButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
- // 取消按键
- self.userOutput.text = @"Clicked 'OK'";
- }];
- // 添加操作(顺序就是呈现的上下顺序)
- [alertDialog addAction:laterAction];
- [alertDialog addAction:neverAction];
- [alertDialog addAction:okAction];
- // 呈现警告视图
- [self presentViewController:alertDialog animated:YES completion:nil];
- }
3个按键分别代表了3种不同类型的按键,分别是默认按键(普通)、销毁按键(红色)和取消按键(粗体)。从代码看其实就是在上一个的基础上加了3个 UIAlertAction 而已,然后分别设置不同的 style,效果如下:
- - (IBAction)doAlertInput:(id)sender {
- // 准备初始化配置参数
- NSString *title = @"Email Address";
- NSString *message = @"Please enter your your email address:";
- NSString *okButtonTitle = @"OK";
- // 初始化
- UIAlertController *alertDialog = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
- // 创建文本框
- [alertDialog addTextFieldWithConfigurationHandler:^(UITextField *textField){
- textField.placeholder = @"Your Email";
- textField.secureTextEntry = NO;
- }];
- // 创建操作
- UIAlertAction *okAction = [UIAlertAction actionWithTitle:okButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
- // 读取文本框的值显示出来
- UITextField *userEmail = alertDialog.textFields.firstObject;
- self.userOutput.text = userEmail.text;
- }];
- // 添加操作(顺序就是呈现的上下顺序)
- [alertDialog addAction:okAction];
- // 呈现警告视图
- [self presentViewController:alertDialog animated:YES completion:nil];
- }
在创建操作前先创建文本框,以便后面的按键可以操作文本框内容。创建文本框也只是用了一个简单的方法而已,想创建更多文本框就再使用多次这个方法即可,程序效果如下:
4. 提醒图表
- - (IBAction)doActionSheet:(id)sender {
- // 准备初始化配置参数
- NSString *title = @"Alert Button Selected";
- NSString *message = @"I need your attention NOW!";
- NSString *okButtonTitle = @"OK";
- NSString *neverButtonTitle = @"Never";
- NSString *laterButtonTitle = @"Maybe Later";
- // 初始化
- UIAlertController *alertDialog = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleActionSheet];
- // 分别3个创建操作
- UIAlertAction *laterAction = [UIAlertAction actionWithTitle:laterButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
- // 普通按键
- self.userOutput.text = @"Clicked 'Maybe Later'";
- }];
- UIAlertAction *neverAction = [UIAlertAction actionWithTitle:neverButtonTitle style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
- // 红色按键
- self.userOutput.text = @"Clicked 'Never'";
- }];
- UIAlertAction *okAction = [UIAlertAction actionWithTitle:okButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
- // 取消按键
- self.userOutput.text = @"Clicked 'OK'";
- }];
- // 添加操作(顺序就是呈现的上下顺序)
- [alertDialog addAction:laterAction];
- [alertDialog addAction:neverAction];
- [alertDialog addAction:okAction];
- // 呈现警告视图
- [self presentViewController:alertDialog animated:YES completion:nil];
- }
效果如图:
IOS8 : UIAlertController的更多相关文章
- IOS8 UIAlertController 弹框
本文转载至 http://blog.csdn.net/jinkaiouyang/article/details/35551769 IOS8中,Apple将UIActionSheet和UIAlertVi ...
- iOS8 UIAlertController弹出框中添加视图(例如日期选择器等等)
UIDatePicker *datePicker = [[UIDatePicker alloc] init]; datePicker.datePickerMode = UIDatePickerMode ...
- 升级IOS8游戏上传自定义头像功能失效的问题
为了支持arm64,之前已经折腾了很久,昨晚打包准备提交苹果审核时,测试那边的同事反馈说游戏上传自定义头像功能不可用了. 游戏上传自定义功能的简介:卡牌游戏最初是<比武招亲>中有一个充VI ...
- UIAlertController custom font, size, color
本文转载至 http://stackoverflow.com/questions/26460706/uialertcontroller-custom-font-size-color up vote2d ...
- iOS改变UIAlertView、UIActionSheet、UIAlertController系统字体颜色
废话不多说,直接上代码,效果是最好的说服力 1.改变UIAlertView字体颜色 [UIView appearance].tintColor = [UIColor greenColor]; 个人还是 ...
- iOS开发之UIAlertController的适配
在iOS8中,只能用UIAlertController.而原来的UIAlertView及UIActionSheet已经被抛弃掉了.但是如果一台iOS 7 的手机运行到有UIAlertControlle ...
- UIActionSheet 修改字体颜色
-(void)willPresentActionSheet:(UIActionSheet *)actionSheet { SEL selector = NSSelectorFromString(@&q ...
- UIAlertViewController+TextField 输入框
if (IOS8) { UIAlertController *alertController=[UIAlertController alertControllerWithTitle:CustomLoc ...
- runtime查找 UIAlertAction 的key 及 UIActionSheet 设置字体颜色
修改不了颜色了 结果发现kvo 的key 不对 哎 直接上代码 设置正确的属性找到对应的key 还以为iOS 11改变了方法 unsigned int count; Ivar *ivars = c ...
随机推荐
- cocos2d-x 定时器selector的使用 :schedule的使用
在游戏设计时,我们需要不断的改变屏幕显示来反映游戏操作的效果,最简单的就是提示用户已经进行的游戏时间.为此,我们需要使用cocos2d-x内置的任务调度机制,即CCNode的schedule成员函数. ...
- zMPLS的安装与配置
1.zmpls的安装 1.1安装环境 ubuntu 12.04 kernel 2.6.35 (对原来的内核进行了替换) 1.2 下载链接 文件zMPLS-0.95-alpha.tar.gz的下载地址为 ...
- Win10還原成最乾淨的狀態 不必重灌
系統不穩定時我們想到的第一個選擇就是重灌,如果你的作業系統是win10將會有另外一個新選擇,就是透過程式進行還原,讓你的電腦回到剛安裝時的清爽. 工具資訊 [軟體名稱]微軟 Refresh Windo ...
- 一个参数引起的mysql从库宕机血案
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://suifu.blog.51cto.com/9167728/1859252 一个参数 ...
- KingbaseES的HA搭建
1.配置资源前准备: 安装好数据库并保持两台机器用户ID及组ID一致,组ID和用户ID在/etc/passwd查看,如不保持一致,可能导致切机时阵列的属主改变,导致数据库无法启动. 建议用法,现在两台 ...
- C# 从字符串向 datetime 转换时失败
更改电脑的日期类型即可,把短日期和长日期修改下面的样子即可:
- jQuery内容过滤器
jQuery内容过滤器 <h1>this is h1</h1> <div id="p1"> <h2>this is h2</h ...
- PHP Memcached 实现简单数据库缓存
Memcache常用方法 Memcache::add — 添加一个值,如果已经存在,则返回false Memcache::addServer — 添加一个可供使用的服务器地址 Memcache::cl ...
- Express4.x安装
1.首先肯定是要安装Node.JS npm install -g expressnpm install -g express-generator 运行express -V输出 4.9.0 2.创建一个 ...
- Pomelo框架
一个典型的多进程MMO运行架构, 如下图所示: pomelo框架的组成如图所示: 架构把游戏服务器做了抽象, 抽象成为两类:前端服务器和后端服务器, 如图: 前端服务器(frontend)的职责: 负 ...