在iOS8之前用UIActionSheet和UIAlertView来提供button选择和提示性信息,比方UIActionSheet能够这样写:

 UIActionSheet *actionSheet = [[UIActionSheet alloc]
initWithTitle:@"title,nil时不显示"
delegate:self
cancelButtonTitle:@"取消"
destructiveButtonTitle:@"确定"
otherButtonTitles:@"第一项", @"第二项",nil];
actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
[actionSheet showInView:self.view];

然后在协议中实现代理:

(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0) {
NSLog(@"确定");
}else if (buttonIndex == 1) {
NSLog(@"第一项");
}else if(buttonIndex == 2) {
NSLog(@"第二项");
}else if(buttonIndex == actionSheet.cancleButtonIndex) {
NSLog(@"取消");
} }
- (void)actionSheetCancel:(UIActionSheet *)actionSheet{ }
-(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{ }
-(void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex{ }

假设须要改动button字体、颜色等能够实现下面代理:

- (void)willPresentActionSheet:(UIActionSheet *)actionSheet {
for (UIView *subViwe in actionSheet.subviews) {
if ([subViwe isKindOfClass:[UILabel class]]) {
UILabel *label = (UILabel *)subViwe;
label.font = [UIFont systemFontOfSize:16];
label.frame = CGRectMake(CGRectGetMinX(label.frame), CGRectGetMinY(label.frame), CGRectGetWidth(label.frame), CGRectGetHeight(label.frame)+20);
}
if ([subViwe isKindOfClass:[UIButton class]]) {
UIButton *button = (UIButton*)subViwe;
if ([button.titleLabel.text isEqualToString:@"确定"]) {
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
} else {
[button setTitleColor:[WTDevice getGreenColor] forState:UIControlStateNormal];
}
button.titleLabel.font = [UIFont systemFontOfSize:18];
}
}
}

以上代码(代理部分)。在ios7及下面版本号中是有效的,可是在iOS8中却不起作用。由于iOS8抛弃了UIActionSheet和UIAlertView,取而代之的是UIAlertController,其用法例如以下(取代UIAlertView):

#ifdef __IPHONE_8_0
if (TARGET_IS_IOS8) {
UIAlertController *actionSheetController = [UIAlertController alertControllerWithTitle:@"提示"
message:@"须要设置同意訪问相机,操作方法见“设置”->“帮助中心”"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:@"确定"
style:UIAlertActionStyleDestructive
handler:^(UIAlertAction * action) {}]; [actionSheetController addAction:actionCancel];
[actionSheetController.view setTintColor:[WTDevice getGreenColor]];
[self presentViewController:actionSheetController animated:YES completion:nil];
}
#endif
if (TARGET_NOT_IOS8) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"须要设置同意訪问相机。操作方法见“设置”->“帮助中心”" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil];
[alert show];
}

取代UIActionSheet:

#ifdef __IPHONE_8_0
if (TARGET_IS_IOS8) {
UIAlertController *actionSheetController = [UIAlertController alertControllerWithTitle:@"action选项"
message:nil
preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *action0 = [UIAlertAction actionWithTitle:@"选项一"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
[self customMethod1];
}];
[actionSheetController addAction:action0]; UIAlertAction *action = [UIAlertAction actionWithTitle:@"选项二"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
[self <span style="font-family: Arial, Helvetica, sans-serif;">customMethod2</span>];
}];
UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"选项三"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
[self customMethod3];
}];
UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:@"取消"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction * action) {}]; [actionSheetController addAction:action];
[actionSheetController addAction:action1];
[actionSheetController addAction:actionCancel];
[actionSheetController.view setTintColor:[UIColor greenColor]];
[self presentViewController:actionSheetController animated:YES completion:nil];
}
#endif
if (TARGET_NOT_IOS8) {
UIActionSheet *as = [[UIActionSheet alloc] initWithTitle:@"action选项" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"选项一",@"选项二",@"选项三", nil];
[as showInView:self.view];
}

至于两者的差别,能够看到。iOS8之前是在controller的view上边又覆盖了一层view,iOS8之后则是present了一个controller而且将代理换成了block,代码显得更加紧凑。

iOS8开发之iOS8的UIAlertController的更多相关文章

  1. IOS开发之IOS8.0最新UIAlertController

    最近苹果更新的IOS8 对以前进行了很大的修改, 更新的API也让人捉急,据说iOS 8的新特性之一就是让接口更有适应性.更灵活,因此许多视图控制器的实现方式发生了巨大的变化.比如全新的UIPrese ...

  2. IOS开发之IOS8.0最新UIAlertController 分类: ios技术 2015-01-20 14:24 144人阅读 评论(1) 收藏

    最近苹果更新的IOS8 对以前进行了很大的修改, 更新的API也让人捉急,据说iOS 8的新特性之一就是让接口更有适应性.更灵活,因此许多视图控制器的实现方式发生了巨大的变化.比如全新的UIPrese ...

  3. iOS开发之UIAlertView与UIAlertController的详尽用法说明

    本文将从四个方面对IOS开发中UIAlertView与UIAlertController的用法进行讲解: 一.UIAlertView与UIAlertController是什么东东? 二.我们为什么要用 ...

  4. iOS开发之UIAlertController的适配

    在iOS8中,只能用UIAlertController.而原来的UIAlertView及UIActionSheet已经被抛弃掉了.但是如果一台iOS 7 的手机运行到有UIAlertControlle ...

  5. iOS开发之WKWebView简单使用

    iOS开发之WKWebView简单使用   iOS开发之 WKWebVeiw使用 想用UIWebVeiw做的,但是突然想起来在iOS8中出了一个新的WKWebView,算是UIWebVeiw的升级版. ...

  6. 李洪强iOS开发之iOS好文章收集

    李洪强iOS开发之iOS好文章收集 该文收集朋友们转发或自己的写的技术文章,如果你也有相关的好文章,欢迎留言,当好文章多的时候,我会对这些好文章进行分门别类 文章 简述 日期 直播服务配置 使用 ng ...

  7. 微信公众号开发之VS远程调试

    目录 (一)微信公众号开发之VS远程调试 (二)微信公众号开发之基础梳理 (三)微信公众号开发之自动消息回复和自定义菜单 前言 微信公众平台消息接口的工作原理大概可以这样理解:从用户端到公众号端一个流 ...

  8. Android混合开发之WebViewJavascriptBridge实现JS与java安全交互

    前言: 为了加快开发效率,目前公司一些功能使用H5开发,这里难免会用到Js与Java函数互相调用的问题,这个Android是提供了原生支持的,不过存在安全隐患,今天我们来学习一种安全方式来满足Js与j ...

  9. Android混合开发之WebView与Javascript交互

    前言: 最近公司的App为了加快开发效率选择了一部分功能采用H5开发,从目前市面的大部分App来讲,大致分成Native App.Web App.Hybrid App三种方式,个人觉得目前以Hybri ...

随机推荐

  1. Android wifi无线调试App新玩法ADB WIFI

    Wifi 调试App已经不是什么新鲜的事情了,之前也看过不少,不是使用麻烦就是需要root权限,今个我给大家介绍一款好用的android studio 插件--ADB WIFI. 安装 setting ...

  2. 以log(n)的时间求矩形内的点

    设想这么一个简单的问题,在一个平面上有n个点,给定一个矩形,问位于矩形内的点有哪些. 这个问题的简单思路非常简单,每次遍历所有点,看其是否在给定的矩形中.时间复杂度呢?单次查询的时间就是一次遍历的时间 ...

  3. GIT 恢复已删除的提交

    在Git中一切的操作都是可以恢复的,包括已经删除的提交,今天在做一个项目时就遇到了这种问题,也不知道是怎么回事不小心就把上午提交的所有代码都删除了,于是赶紧通过git log来查看日志,但奇怪的是当时 ...

  4. [Node.js]29. Level 6: Socket.io: Setting up Socket.io server-side & Client socket.io setup

    Below we've already created an express server, but we want to start building a real-time Q&A mod ...

  5. 【Canvas】动态正17边光阑 向高斯致敬

    [背景知识] 公元前三世纪,欧几里得在<几何原本>中记载了正方形,正五边形,正六边形的做法,后来人们也掌握了正十五边形作图,但之后两千多年,人们没有在更高阶边形上取得突破. 1796年,1 ...

  6. python使用md5处理下载图片

    import urllib2 import hashlib opener = urllib2.build_opener() req = opener.open("http://avatar. ...

  7. BIO、NIO、AIO差别

    网上非常多IO资料,对新手来说.越看越晕.依据自己的理解.总结对照了一下BIO.NIO.AIO. BIO:线程发起IO请求,无论内核是否准备好IO操作,从发起请求起,线程一直堵塞,直到操作完毕. 例如 ...

  8. 算法笔记_119:蓝桥杯第六届省赛(Java语言A组)试题解答

     目录 1 熊怪吃核桃 2 星系炸弹 3 九数分三组 4 循环节长度 5 打印菱形 6 加法变乘法 7 牌型种数 8 移动距离 9 垒骰子 10 灾后重建   前言:以下试题解答代码部分仅供参考,若有 ...

  9. 算法笔记_127:蓝桥杯2017模拟赛-本科组习题解答(Java)

     目录 1 算年龄 2 猜算式 3 排列序数 4 字符串比较 5 还款计算 6 滑动解锁 7 风险度量   PS:以下代码部分仅供参考,若有不当之处,还请路过同学指出哦~ 1 算年龄 标题:算年龄 英 ...

  10. 减小App包的大小

    检查.ipa文件 首先获得app的ipa文件. 将ipa文件的后缀改为.zip,解压得到包内容. 查看资源文件哪个最大.然后试着对最大的文件即可处理 图片 尽量使用8-bit图片 使用8-bit的PN ...