美文网首页iOS DeveloperiOS 开发
解决iOS8以下UIAlertController无法使用的问题

解决iOS8以下UIAlertController无法使用的问题

作者: 欣东 | 来源:发表于2016-07-26 17:35 被阅读112次

    XDAlertController

    最近在一个项目中遇到的这样的一个问题iOS8以下UIAlertController无法使用,XDAlertController我的一个解决方案

    实现原理

    在iOS7环境下调用UIAlertController会崩溃,所以通过判断系统的版本号来调用不同的API;
    通过Method Swizzling替换原生的presentViewController和提供近似于原生API接口,让开发者感觉不出与原生API有什么差别。(Method Swizzling通过method_exchangeImplementations交换方法,交换时期在调用load的时候,里面涉及runtime相关知识,如果理解起来吃力,可以移步到这边文章了解

    使用

    #import "XDAlertController.h"
    ...
    XDAlertController *alert = [XDAlertController alertControllerWithTitle:@"我是actionsheet" message:@"123" preferredStyle:XDAlertControllerStyleActionSheet];
        XDAlertAction *action1 = [XDAlertAction actionWithTitle:@"default" style: XDAlertActionStyleDefault handler:^( XDAlertAction * _Nonnull action) {
            
        }];
        
        XDAlertAction *action2 = [XDAlertAction actionWithTitle:@"取消" style:XDAlertActionStyleCancel handler:^(XDAlertAction * _Nonnull action) {
            
        }];
        
        XDAlertAction *action3 = [XDAlertAction actionWithTitle:@"destructive" style:XDAlertActionStyleDestructive handler:^(XDAlertAction * _Nonnull action) {
            
        }];
        
        [alert addAction:action1];
        [alert addAction:action2];
        [alert addAction:action3];
        
        [self presentViewController:alert animated:YES completion:nil];
    
    

    github地址

    https://github.com/caixindong/XDAlertController
    大家觉得喜欢就赏个star,有什么问题可以issue我或者在评论指出,相互学习

    相关文章

      网友评论

        本文标题:解决iOS8以下UIAlertController无法使用的问题

        本文链接:https://www.haomeiwen.com/subject/bmnnjttx.html