iOS8开发之iOS8的UIAlertController
在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的更多相关文章
- IOS开发之IOS8.0最新UIAlertController
最近苹果更新的IOS8 对以前进行了很大的修改, 更新的API也让人捉急,据说iOS 8的新特性之一就是让接口更有适应性.更灵活,因此许多视图控制器的实现方式发生了巨大的变化.比如全新的UIPrese ...
- IOS开发之IOS8.0最新UIAlertController 分类: ios技术 2015-01-20 14:24 144人阅读 评论(1) 收藏
最近苹果更新的IOS8 对以前进行了很大的修改, 更新的API也让人捉急,据说iOS 8的新特性之一就是让接口更有适应性.更灵活,因此许多视图控制器的实现方式发生了巨大的变化.比如全新的UIPrese ...
- iOS开发之UIAlertView与UIAlertController的详尽用法说明
本文将从四个方面对IOS开发中UIAlertView与UIAlertController的用法进行讲解: 一.UIAlertView与UIAlertController是什么东东? 二.我们为什么要用 ...
- iOS开发之UIAlertController的适配
在iOS8中,只能用UIAlertController.而原来的UIAlertView及UIActionSheet已经被抛弃掉了.但是如果一台iOS 7 的手机运行到有UIAlertControlle ...
- iOS开发之WKWebView简单使用
iOS开发之WKWebView简单使用 iOS开发之 WKWebVeiw使用 想用UIWebVeiw做的,但是突然想起来在iOS8中出了一个新的WKWebView,算是UIWebVeiw的升级版. ...
- 李洪强iOS开发之iOS好文章收集
李洪强iOS开发之iOS好文章收集 该文收集朋友们转发或自己的写的技术文章,如果你也有相关的好文章,欢迎留言,当好文章多的时候,我会对这些好文章进行分门别类 文章 简述 日期 直播服务配置 使用 ng ...
- 微信公众号开发之VS远程调试
目录 (一)微信公众号开发之VS远程调试 (二)微信公众号开发之基础梳理 (三)微信公众号开发之自动消息回复和自定义菜单 前言 微信公众平台消息接口的工作原理大概可以这样理解:从用户端到公众号端一个流 ...
- Android混合开发之WebViewJavascriptBridge实现JS与java安全交互
前言: 为了加快开发效率,目前公司一些功能使用H5开发,这里难免会用到Js与Java函数互相调用的问题,这个Android是提供了原生支持的,不过存在安全隐患,今天我们来学习一种安全方式来满足Js与j ...
- Android混合开发之WebView与Javascript交互
前言: 最近公司的App为了加快开发效率选择了一部分功能采用H5开发,从目前市面的大部分App来讲,大致分成Native App.Web App.Hybrid App三种方式,个人觉得目前以Hybri ...
随机推荐
- 模态框在IE下的问题,即position:fixed在IE下不兼容的处理方式
项目中遇到的问题,模态框在IE下总出现如图所示双层遮罩框,经排查发现是由于bootstrap里写的modal的样式里position:fixed不兼容IE的原因,导致铺不满整个窗口. 解决方案:在项目 ...
- ExcelReader(解析Excel的工具类)
package cn.com.css.common.util; import java.io.IOException; import java.io.InputStream; import java. ...
- 编译和使用 MySQL C++ Connector
记录编译 mysql C and C++ connector 和简单访问数据库. 环境: vs2012, mysql 5.6.13, 基于x64 0. 软件包 mysql http://dev.my ...
- 五条强化 SSH 安全的建议
当你查看你的 SSH 服务日志,可能你会发现充斥着一些不怀好意的尝试性登录.这里有 5 条常规建议(和一些个别特殊策略)可以让你的 OpenSSH 会话更加安全. 强化密码登录 密码登录很方便,因为你 ...
- 反汇编基本原理与x86指令构造
反汇编基本原理与x86指令构造 概要:旨在讲述程序的二进制代码转换到汇编.即反汇编的基本原理.以及 x86 架构的 CPU 的指令构造,有这个基础后就能够自己编写汇编程序了,也能够将二进制代码数据转换 ...
- 虎嗅: 小米盒子vs乐视盒子
机顶盒并非新鲜概念,可一旦和互联网发生了跨界关系,就会产生奇妙的反应.自年初小米盒子和乐视盒子分别在突破重重阻碍成功发售之后,互联网企业进军硬件制造领域的趋势愈发明显.今天我们拿到了两家的盒子产品,从 ...
- h5 微场景
兔展: http://www.rabbitpre.com/ 易企秀: http://www.eqxiu.com/site/show 云来: http://www.liveapp.cn/
- 【线程篇】stop() 和suspend()
1.为什么不推荐用 stop()和 suspend() stop这个方法将终止所有未结束的方法,包括run方法.当一个线程停止时候,他会立即释放所有他锁住对象上的锁.这会导致对象处于不一致的状态.假如 ...
- List(T)类的方法
List<T>.Clear 方法 List<T>.RemoveAll 方法 http://msdn.microsoft.com/zh-cn/library/s6hkc2c4(v ...
- dyld: Library not loaded: @rpath/XCTest.framework/XCTest Referenced from: /private/var/mobile/Conta
dyld: Library not loaded: @rpath/XCTest.framework/XCTest Referenced from: /private/var/mobile/Cont ...