IOS8以上版本,使用UIAlertController代替 UIActionSheet和UIAlertView

 

IOS8以上版本,使用UIAlertController代替 UIActionSheet和UIAlertView

标签: IOSUIAlertControllerUIActionSheet按钮颜色
  9026人阅读  评论(0)  收藏  举报
  分类:

苹果在IOS8版本上,新添加了一个UIAlertController用来代替 UIActionSheet 和 UIAlertView;

在工作中,会遇到修改这两个控件按钮颜色的要求,在网上一看,多是采用下边这种方法的:

- (void)willPresentActionSheet:(UIActionSheet *)actionSheet

{

    DDLogDebug(@"%@",actionSheet.subviews);

    for (UIView *subview in actionSheet.subviews) {

        if ([subview isKindOfClass:[UIButton class]]) {

            UIButton *button = (UIButton *)subview;

            

            [button setTitleColor:APP_TINT_COLOR forState:UIControlStateHighlighted];

            [button setTitleColor:APP_TINT_COLOR forState:UIControlStateNormal];

            [button setTitleColor:APP_TINT_COLOR forState:UIControlStateSelected];

        }

    }

}

但是在IOS8以后,无论是UIActionSheet、UIAlertView还是其他的一些控件的subviews就一直是空,不能返回任何UIView。所以在程序中根据系统版本采用不同的实现方式才是行之有效的,下面是UIAlertController的简单使用:

if ([[XWGlobalHelper systemVersionintValue] > 7.99) {

        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil

                                                                                 message:nil

                                                                          preferredStyle:UIAlertControllerStyleActionSheet];      

        UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel

                                                            handler:^(UIAlertAction * action) {}];

        UIAlertAction* fromPhotoAction = [UIAlertAction actionWithTitle:@"从相册选择" style:UIAlertActionStyleDefault                                                                 handler:^(UIAlertAction * action) {                                                                     UIImagePickerController *imagePicker = [[UIImagePickerControllerallocinit];

                                                                     imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

                                                                     imagePicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;

                                                                     //        imagePicker.allowsEditing = YES;

                                                                     imagePicker.allowsEditing = NO;

                                                                     imagePicker.delegate = self;

                                                                     [self presentViewController:imagePicker animated:YES completion:nil];         

                                                                 }];

        UIAlertAction* fromCameraAction = [UIAlertAction actionWithTitle:@"相机" style:UIAlertActionStyleDefault                                                            handler:^(UIAlertAction * action) {            

if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])   

                                                              {

                                                                     UIImagePickerController *imagePicker = [[UIImagePickerController allocinit];

                                                                    imagePicker.delegate = self;

                                                                     //            imagePicker.allowsEditing = YES;

                                                                     imagePicker.allowsEditing = NO;

                                                                     imagePicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;

                                                                     imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;

                                                                     [self presentViewController:imagePicker animated:YES completion:nil];

                                                                 }

                                                             }];

        [alertController addAction:cancelAction];

        [alertController addAction:fromCameraAction];

        [alertController addAction:fromPhotoAction];

        [self presentViewController:alertController animated:YES completion:nil];

    } else {

        UIActionSheet * actionSheet = [[UIActionSheet allocinitWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nilotherButtonTitles:@"相机",@"从相册选择"nil];

        actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;

        actionSheet.delegate = self;

        [actionSheet showInView:self.view];

    }

上边的UIAlertControllerStyleActionSheet指定了显示效果为之前版本的 UIActionSheet,另外还有一个UIAlertControllerStyleAlert指定了显示效果为之前版本的UIAlertView!
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值