在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. CentOs中mysql服务器重置root密码方法

    1. 停止mysql: service mysqld stop 2. 编辑/etc/my.cnf,在[mysqld]这行后面加上skip-grant-tables ,并保存 3. 启动mysql: s ...

  2. angular6 Can't bind to 'zzst' since it isn't a known property of

    文档: https://angular.io/guide/template-syntax#event-binding The Angular compiler may reject these bin ...

  3. PHP 使用mysql 与 mysqli 连接Mysql数据库

    代码很简单直接上了 <?php /** * @Author: HTL * @Email: Huangyuan413026@163.com * @DateTime: 2015-05-14 16:0 ...

  4. Discuz 学习笔记一 :getgdc 和get_client_ip

    Getgdc函数 discuz有一个超级变量的自定义函数:   function getgpc($k, $type='GP') {     $type = strtoupper($type);     ...

  5. 免费的HTML商业模板-Hidayah

    在线演示 在线演示 本地下载 在这款模板中,你可以随意缩放大小,适合笔记本.平板电脑或手机,非常方便阅读.

  6. Android面试总结经

    自上周怒辞职以后,就開始苦逼的各种面试生涯,生活全然靠私活来接济,时有时没有,真难.还能快乐的玩耍吗.最多一天面试了5家,哎感觉都是不急招人,各种等待通知.好不easy等来一家.还克扣了薪资,从我要的 ...

  7. 如何使用千千静听为MP3添加专辑封面和文字信息

    使用千千静听播放器打开某MP3文件,右击该文件,选择属性. 2 点击专辑封面即可添加或更换专辑封面 点击保存到文件再点击重新读取文件即可发现有效了 3 为MP3批量添加添加封面 选中播放列表的所有文件 ...

  8. mysqld: unrecognized service

    为了快速搭建了测试环境yum安装了MySQL. 执行 [root@localhost bin]#mysql ERROR 2002 (HY000): Can't connect to local MyS ...

  9. windows 配置ftp server

  10. 解决document.location.href下载文件时中文乱码

    1:tomcat 安装路径下 找到 conf文件下的server.xml 2:<Connector port="8080" URIEncoding="utf-8&q ...