iOS8中提示框的使用UIAlertController(UIAlertView和UIActionSheet二合一)

本文转载至 http://blog.csdn.net/liuwuguigui/article/details/39494597
IOS8UIAlertViewUIActionSheet

iOS8推出了几个新的“controller”,主要是把类似之前的UIAlertView变成了UIAlertController,这不经意的改变,貌似把我之前理解的“controller”一下子推翻了~但是也无所谓,有新东西不怕,学会使用了就行。接下来会探讨一下这些个新的Controller。

 

复制代码
- (void)showOkayCancelAlert {NSString *title = NSLocalizedString(@"A Short Title Is Best", nil);NSString *message = NSLocalizedString(@"A message should be a short, complete sentence.", nil);NSString *cancelButtonTitle = NSLocalizedString(@"Cancel", nil);NSString *otherButtonTitle = NSLocalizedString(@"OK", nil);UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];// Create the actions.UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {NSLog(@"The \"Okay/Cancel\" alert's cancel action occured.");}];UIAlertAction *otherAction = [UIAlertAction actionWithTitle:otherButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {NSLog(@"The \"Okay/Cancel\" alert's other action occured.");}];// Add the actions.[alertController addAction:cancelAction];[alertController addAction:otherAction];[self presentViewController:alertController animated:YES completion:nil];
}
复制代码

这是最普通的一个alertcontroller,一个取消按钮,一个确定按钮。

新的alertcontroller,其初始化方法也不一样了,按钮响应方法绑定使用了block方式,有利有弊。需要注意的是不要因为block导致了引用循环,记得使用__weak,尤其是使用到self。

上面的界面如下:

如果UIAlertAction *otherAction这种otherAction多几个的话,它会自动排列成如下:

另外,很多时候,我们需要在alertcontroller中添加一个输入框,例如输入密码:

这时候可以添加如下代码:

 [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {// 可以在这里对textfield进行定制,例如改变背景色textField.backgroundColor = [UIColor orangeColor];}];

而改变背景色会这样:

完整的密码输入:

复制代码
- (void)showSecureTextEntryAlert {NSString *title = NSLocalizedString(@"A Short Title Is Best", nil);NSString *message = NSLocalizedString(@"A message should be a short, complete sentence.", nil);NSString *cancelButtonTitle = NSLocalizedString(@"Cancel", nil);NSString *otherButtonTitle = NSLocalizedString(@"OK", nil);UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];// Add the text field for the secure text entry.[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {// Listen for changes to the text field's text so that we can toggle the current// action's enabled property based on whether the user has entered a sufficiently// secure entry.[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleTextFieldTextDidChangeNotification:) name:UITextFieldTextDidChangeNotification object:textField];textField.secureTextEntry = YES;}];// Create the actions.UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {NSLog(@"The \"Secure Text Entry\" alert's cancel action occured.");// Stop listening for text changed notifications.[[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:alertController.textFields.firstObject];}];UIAlertAction *otherAction = [UIAlertAction actionWithTitle:otherButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {NSLog(@"The \"Secure Text Entry\" alert's other action occured.");// Stop listening for text changed notifications.[[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:alertController.textFields.firstObject];}];// The text field initially has no text in the text field, so we'll disable it.otherAction.enabled = NO;// Hold onto the secure text alert action to toggle the enabled/disabled state when the text changed.self.secureTextAlertAction = otherAction;// Add the actions.[alertController addAction:cancelAction];[alertController addAction:otherAction];[self presentViewController:alertController animated:YES completion:nil];
}
复制代码

注意四点:

1.添加通知,监听textfield内容的改变:

复制代码
// Add the text field for the secure text entry.[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {// Listen for changes to the text field's text so that we can toggle the current// action's enabled property based on whether the user has entered a sufficiently// secure entry.[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleTextFieldTextDidChangeNotification:) name:UITextFieldTextDidChangeNotification object:textField];textField.secureTextEntry = YES;}];
复制代码

2.初始化时候,禁用“ok”按钮:

otherAction.enabled = NO;

self.secureTextAlertAction = otherAction;//定义一个全局变量来存储

3.当输入超过5个字符时候,使self.secureTextAlertAction = YES:

- (void)handleTextFieldTextDidChangeNotification:(NSNotification *)notification {UITextField *textField = notification.object;// Enforce a minimum length of >= 5 characters for secure text alerts.self.secureTextAlertAction.enabled = textField.text.length >= 5;
}

4.在“OK”action中去掉通知:

复制代码
UIAlertAction *otherAction = [UIAlertAction actionWithTitle:otherButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {NSLog(@"The \"Secure Text Entry\" alert's other action occured.");// Stop listening for text changed notifications.[[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:alertController.textFields.firstObject];}];
复制代码

 

最后是以前经常是alertview与actionsheet结合使用,这里同样也有:

复制代码
- (void)showOkayCancelActionSheet {NSString *cancelButtonTitle = NSLocalizedString(@"Cancel", nil);NSString *destructiveButtonTitle = NSLocalizedString(@"OK", nil);UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];// Create the actions.UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {NSLog(@"The \"Okay/Cancel\" alert action sheet's cancel action occured.");}];UIAlertAction *destructiveAction = [UIAlertAction actionWithTitle:destructiveButtonTitle style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {NSLog(@"The \"Okay/Cancel\" alert action sheet's destructive action occured.");}];// Add the actions.[alertController addAction:cancelAction];[alertController addAction:destructiveAction];[self presentViewController:alertController animated:YES completion:nil];
}
复制代码

在底部显示如下:

 

好了,至此,基本就知道这个新的controller到底是怎样使用了。

转载于:https://www.cnblogs.com/Camier-myNiuer/p/4021604.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/407019.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

嵌入式学习路线

ARMLINUX路线,主攻嵌入式Linux操作系统及其上应用软件开发目标: (1)掌握主流嵌入式微处理器的结构与原理(初步定为arm9) (2)必须掌握一个嵌入式操作系统(初步定为uclinux…

Taro+react开发(47)taro中消息机制

import Taro, { Events } from tarojs/taroconst events new Events()// 监听一个事件,接受参数 events.on(eventName, (arg) > {// doSth })// 监听同个事件,同时绑定多个 handler events.on(eventName, handler1) events.on(eventName, handler2) …

纯js监听滚动条到底部(vue版)

项目中,因为数据量过大,为提高页面性能,采用页面滑动 滚动条到底部的时候再进行数据请求分页,这边给大给个核心,结合vue的生命周期用纯javascript写的一个监听函数 第一个我们需要知道几个属性值,判断滚动…

记腾讯互娱网站布局(1)

1.导航栏 第一步:设置最外层的容器的定位方式、宽度和高度以及背景 第二步:给第二层容器设置内容的居中显示 第三步:设置居中的logo的定位和位置 第四步:设置6个标志的布局 设置所有的导航栏项目的定位和距离顶部的距离&#xff0…

第五——十三章的作业

第五章 1.团队模式和团队的开发模式有什么关系? 答:团队模式指团队的分工模式,团队内部的结构,团队开发模式指团队开发的流程及步骤 2.如果你领头开展一个全新的项目,你要怎么选择“合适”的团队模式? 答&a…

Taro+react开发(48)taro中switchTab

跳转到 tabBar 页面,并关闭其他所有非 tabBar 页面

JS中完美兼容各大浏览器的scrolltop方法

1、各浏览器下 scrollTop的差异 IE6/7/8/9/10: 对于没有doctype声明的页面里可以使用 document.body.scrollTop 来获取 scrollTop高度 ; 对于有doctype声明的页面则可以使用 document.documentElement.scrollTop ; Safari: safari 比较特…

记腾讯互娱网站布局(2)

2.头图特效 给头图设置宽度100%以及定高1110px,同时设置display为table,和定位方式fixed 通过设置绝对定位以及固定宽高和背景的方式来实现 存放动态特效的盒子采用绝对定位,并且触发流体特效以及百分百宽的方式 主图上标志的实现是采用外部容…

DB Reindex

数据库在使用一段时间后,就会出现很多的索引碎片。declare ID int set IDOBJECT_ID(SMT_QC) dbcc showcontig(ID)Scan Density值越低代表越需做DBCC ReIndex ,刚做完DBCC会等于 ReIndex100% 下面是Reindex的存储CREATE procedure [dbo].[DBReindex] DB varchar(20)…

ABBYY

ABBYY FineReader Engine泰比OCR文字识别控件移动版 产品功能:OMR识别控件 平台: 开发商:ABBYY”‘Š€ 版本:产品介绍:手机识别的高品质和精度 泰比(ABBYY)Mobile OCR Engine是基于对世界知名的…

JS预编译过程

首先讲预编译过程 JS代码执行过程三部曲 过程 语法分析:首先扫描一遍,看有没有低级的语法错误预编译解释执行:解释一行执行一行 预编译low讲 函数声明整体提升,变量 的声明提升(这个其实很low,点击low…

codeforces 476B.Dreamoon and WiFi 解题报告

题目链接:http://codeforces.com/problemset/problem/476/B 题目意思:给出两个字符串str1, str2,其中,str1 只由 和 - 组成,而str2 由 ,-和?组成。初始点在原点0的位置,经过 str1 的变换最终会…

在应用程序级别以外使用注册为 allowDefinition='MachineToApplication' 的节是错误

在应用程序级别以外使用注册为 allowDefinitionMachineToApplication 的节是错误 在web.config文件之外注册为 allowDefinitionMachineToApplication 的节是错误 遇到这个问题,我真是晕啊! 以下是我个人的经验解决上述的二个问题,至今有时还…

bash删除文件中的空行

方法一:sed /^$/d a.txt 所以如果要将删除后的结果替换原文件中的内容,就可以用: sed /^$/d a.txt > temp; mv temp a.txt 然后在弹出提示符下选择Y就可以了。 方法二:在vi命令提示符下,输入:%s/^\n/…

Taro+react开发(50) 小程序触底操作

onReachBottom() {console.log("我在触底");const { pageIndex, pageSize, getStauList } this.state;if (pageIndex * pageSize > getStauList.length) return;this.setState({pageIndex: pageIndex 1},() > {this.getStatusList();});}

记腾讯互娱网站布局(3)

3.图文回顾 先看看整个网站的全貌 这里’display:table;width:100%;table-layout:fixed’是约定俗成的写法,用来保证固定的表单布局,同时让连续的英文单词不会超出 内部采用’display:table-cell’属性将内容当做表格的td标签一样显示,设置v…

JS阻止冒泡和取消默认事件(默认行为)

阻止事件冒泡 function stopPropagat(e) {if (e && e.stopPropagation) {e.stopPropagation();//标准浏览器} else {window.event.cancelBubble true;//兼容IE的方式来取消事件冒泡}}阻止默认行为 function stopDefault(e) {if (e && e.preventDefault) {e.pr…

如何提高个人的职涯“本钱”

何所谓职业生涯的本钱?简单来说,必须涵盖三个方面,一是充分的能力,包括专业技能、管理知识的储备等;二是态度,即工作态度及风格是否契合你所希望就职公司的文化,开阔的视野、兼容并包的心胸、善…

Taro+react开发(51) 数组对象和数组得处理

for (var key of value) {arrcode.push(selectorIndustry.find(obj > obj.value key));}

简单的python流回显服务器与客户端

环境:Fedora12 python2.6.2 server.py #!/usr/bin/python import socket srvsock socket.socket(socket.AF_INET, socket.SOCK_STREAM) srvsock.bind((, 5000))srvsock.listen(5) while True:clisock, (remoteHost, remotePort) srvsock.accept()str11 clisock…