博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS8开发之iOS8的UIAlertController
阅读量:7288 次
发布时间:2019-06-30

本文共 6004 字,大约阅读时间需要 20 分钟。

在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 customMethod2];                                                       }];        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,代码显得更加紧凑。

转载地址:http://qtpjm.baihongyu.com/

你可能感兴趣的文章
一个效果不错的Java Swing模拟屏幕截图工具类
查看>>
MySQL 的主从复制
查看>>
把合同中红色印章实现打印不显示方法
查看>>
linux调优工具使用
查看>>
php.ini中开启段标签
查看>>
php-扩展编译安装扩展(通用版)
查看>>
信号槽的实现实例—— Qt 和 Boost
查看>>
一段简单的php翻页代码
查看>>
AMD峰会:AMD继续领先intel 并走在节能前沿
查看>>
MySQL第三方复制工具 --- Tungsten-Replicator
查看>>
软件平台与框架的生命周期
查看>>
mysql 引擎MyISAM 和 InnoDB区别
查看>>
Docker(二十)在 Kubernetes 中配置私有 DNS 和上游域名服务器
查看>>
AIX 6.1 + HACMP 6.1 + Oracle 11g双机实施 (1) --- AIX 6.1配置HACMP 6.1
查看>>
我的友情链接
查看>>
mysqldump 使用
查看>>
做最好的自己,人生十件事(事业,人生,情感)
查看>>
jboss 优化
查看>>
Android OpenGL ES与EGL
查看>>
python中urllib和urllib2小结
查看>>