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我或者在評論指出,相互學習