在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. 同时启动多个Tomcat服务器

    以下步骤能够同时启动两个tomcat:1.特别要注意:不要设置CATALINA_HOME 2.分别修改安装目录下的conf子目录中的server.xml文件: a.修改http访问端口(默认为8080 ...

  2. eclipse无法解析导入 java.util

    eclipse无法解析导入 java.util是因为jre配置错误. 1.点击需要导入jar的项目,右击项目属性(properties),进入到如下图界面: 2.选择Java Build Path选项 ...

  3. WordPress 如何搜索文章内容而不搜索页面

    如何在WordPress 中只搜索指定的文章类型?在http://www.wpbeginner.com上了解到通过WP提供的钩子"pre_get_posts"方法可能实现 该钩子方 ...

  4. 如何将数据转换libsvm格式文件

    原文:http://blog.sina.com.cn/s/blog_5c2f929b0100qse8.html 有三种工具可用1.网上有一个xls文FormatDataLibsvm.xls具有宏命令, ...

  5. Java IO--字节-字符流转换

    OutputStreamWriter和InputStreamReader 一般在操作输入输出内容的就需要使用字节或字符流,但是有些时候需要将字符流变为字节流的形式,或者将字节流变为字符流的形式,所以, ...

  6. The connection to adb is down, and a severe error has occured. 错误

    错误:The connection to adb is down, and a severe error has occured. [2010-03-11 09:36:56 - HelloOPone] ...

  7. PHP MySQL -处理语句

    PHP MySQL 预处理语句 预处理语句对于防止 MySQL 注入是非常有用的. 预处理语句及绑定参数 预处理语句用于执行多个相同的 SQL 语句,并且执行效率更高. 预处理语句的工作原理如下: 预 ...

  8. 常见的前端UI框架

    Bootstrap 首先说 Bootstrap,估计你也猜到会先说或者一定会有这个( 呵呵了 ),这是说明它的强大之处,拥有框架一壁江山的势气.自己刚入道的时候本着代码任何一个字母都得自己敲出来挡我者 ...

  9. EXCEL 列与列怎么交换?

    选中A列数据,按先SHIFT键的同时按住鼠标左键,向右拖动鼠标,在拖动的过程中,会出现一条虚线,当拖动到B列的右边缘时,屏幕上会出现 C:C 的提示,这时送开SHIFT键及鼠标左键,就完成了A B两列 ...

  10. 【转】SQL2008的sa账户被禁用,其他账户无法连接的解决方法

    或者你还有其它的sysadmin权限的账号,你可以用此账号登录,重置SA密码. 但是在以下情况下,怎么办呢? 1. SA密码丢失或者SA账号被禁用. 2. 你进行了一些安全操作,把BuiltinAdm ...