一个iPhone X的适配让楼主受尽了自定义的苦,使用系统API多好,所以在楼主不懈的努力下,终于和组长达成一致:逐步用系统控件替换代码里面的自定义控件,第一个挨刀的就是 BlockAlertsAnd-ActionSheets,不是楼主不喜欢它或者它写的不好,而是因为这个好替换,可是啊,楼主维护的App代码量大概在100万行左右,全都写成系统的也会吐啊!,下面先介绍下系统的样子。

iOS8.0,苹果开放:UIAlertController 并在之后两个版本放弃了对UIAlertView(9.0)和 UIActionSheet(8.3)的维护。

NS_CLASS_DEPRECATED_IOS(2_0, 9_0, "UIAlertView is deprecated. Use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert instead") __TVOS_PROHIBITED
@interface UIAlertView : UIView
@end NS_CLASS_DEPRECATED_IOS(2_0, 8_3, "UIActionSheet is deprecated. Use UIAlertController with a preferredStyle of UIAlertControllerStyleActionSheet instead") __TVOS_PROHIBITED
@interface UIActionSheet : UIView
@end

UIAlertController 是UIAlertView &UIActionSheet的合集,通过UIAlertControllerStyle来确定是Alert or ActionSheet

NS_CLASS_AVAILABLE_IOS(8_0) @interface UIAlertController : UIViewController

+ (instancetype)alertControllerWithTitle:(nullable NSString *)title message:(nullable NSString *)message preferredStyle:(UIAlertControllerStyle)preferredStyle;

 - (void)addAction:(UIAlertAction *)action;
@property (nonatomic, readonly) NSArray<UIAlertAction *> *actions; @property (nonatomic, strong, nullable) UIAlertAction *preferredAction NS_AVAILABLE_IOS(9_0); - (void)addTextFieldWithConfigurationHandler:(void (^ __nullable)(UITextField *textField))configurationHandler;
@property (nullable, nonatomic, readonly) NSArray<UITextField *> *textFields; @property (nullable, nonatomic, copy) NSString *title;
@property (nullable, nonatomic, copy) NSString *message; @property (nonatomic, readonly) UIAlertControllerStyle preferredStyle; @end

UIAlertController 要求初始化的时候就指明类型(preferredStyle 是 readonly),尽管API设计的相当简单,但是用起来总是有点不太舒服,如下:

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:style];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"OK");
}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"OK");
}];
[alertController addAction:okAction];
[alertController addAction:cancelAction];
[self /** self isKindof:UIViewController */presentViewController:alertController animated:YES /** must be YES*/ completion:nil];

楼主想哭,可是自己找的锅,累死也要搞定,所以就着手开始处理,整了3个我就开始觉得不是办法了,想了下,楼主决定动手改造下系统API,楼主不会打自己脸的,所以也不是瞎折腾,也不是自定义,只是改了系统API的调用方式。

其实还是系统的!!!!
其实还是系统的!!!!
其实还是系统的!!!!
下面上一段代码看看怎么使用!

[KDotAlert alert].format(@"Cancel", @"There is Cancel")
.action(@"OK", ^(UIAlertAction * _Nonnull action) {
[self showMore:@"OK"];
}).cancel(@"Cancel", nil).show(self, nil);

至于:[self showMore:@"OK"]; self是安全的,不需要你加weak

看下定义:

NS_CLASS_AVAILABLE_IOS(8_0) @interface KDotAlert : NSObject

@property (nonatomic, strong, readonly) KDotAlertFormat format;         // format title & message
@property (nonatomic, strong, readonly) NSString * _Nullable title; // title
@property (nonatomic, strong, readonly) NSString * _Nullable message; // message
@property (nonatomic, assign, readonly) UIAlertControllerStyle preferredStyle; // The style of the alert controller. @property (nonatomic, strong, readonly) KDotAlertAction action; // Attaches an action object to the alert or action sheet. default style;
@property (nonatomic, strong, readonly) KDotAlertAction cancel; // ... cancel style;
@property (nonatomic, strong, readonly) KDotAlertAction destructive;// ... destructive style; // The actions that the user can take in response to the alert or action sheet.
@property (nonatomic, strong, readonly) NSArray<UIAlertAction *> * _Nullable actions; // Make the preferred action for the user to take from an alert.
@property (nonatomic, strong, readonly) KDotAlertPreferred preferred NS_AVAILABLE_IOS(9_0); @property (nonatomic, strong, readonly) KDotAlertTextField textField; // Adds a text field to an alert;
@property (nonatomic, strong, readonly) NSArray<UITextField *> * _Nullable textFields; // The array of text fields displayed by the alert. @property (nonatomic, strong, readonly) KDotAlertShow show; // show with viewController + (instancetype _Nonnull )alert;
+ (instancetype _Nonnull )actionSheet; @end

基本上支持了所有系统API...
算了,不吹了,上Github连接;

KDotAlert的更多相关文章

随机推荐

  1. 【effective c++】定制new和delete

    条款49: 了解new-handler的行为 operator new 和 operator delete只适合用来分配单一对象.array所用的内存由operator new[]分配出来,并由ope ...

  2. Swift标识符和keyword

    不论什么一种计算机语言都离不开标识符和keyword,下面我们将具体介绍Swift标识符和keyword. 标示符 标识符就是给变量.常量.方法.函数.枚举.结构体.类.协议等指定的名字.构成标识符的 ...

  3. BUPT复试专题—寻找i*j=m的个数(2016)

    题目描述 3*3的矩阵内容. 1 2 3 2 4 6 3 6 9 即a[i][j](1<=i<=n,1<=j<=n)=i*j. 问一个这样n*n的矩阵里面,里面m出现的次数. ...

  4. [cocos2d-x]怎样降低cocos2d-x游戏的耗电量?

    Cocos2d-x游戏的耗电量一直是个让人头疼的问题,一个简单的三消游戏,玩一会手机就热得发烫,更郁闷的是电池消耗非常快.基本上两个小时就能够把电池耗光. 近期又看到一个帖子.有个老外用cocos2d ...

  5. java内部类的一些看法

    java内部类, 我在看<thinking in java>的时候总感觉模棱两可的, 挣扎了好几天之后, 感觉有一部分的问题想的清楚了, 写一个随笔记录一下, 以备以后修改和查看 什么是内 ...

  6. 项目问题总结2:GUID区分大写和小写吗?

    问题描写叙述: 近期在做项目的过程中,遇到一个问题,将从基础系统查询出来的课程ID作为參数去考评系统里查询考试信息,却什么也查不出来,调试了半天不知道什么原因. 问题分析: 静下心来思考一下,能够肯定 ...

  7. 项目Beta冲刺(团队4/7)

    项目Beta冲刺(团队4/7) 团队名称: 云打印 作业要求: 项目Beta冲刺(团队) 作业目标: 完成项目Beta版本 团队队员 队员学号 队员姓名 个人博客地址 备注 221600412 陈宇 ...

  8. 【项目发起】千元组装一台大型3D打印机全教程(一)前言

    前言 最近又碰到了大尺寸模型打样的需求,我这台17cm直径的kossel mini就捉襟见肘了.怎么办呢,这个时候kossel的好就体现出来了,随意扩展,那么就自己做个kossel-max吧.为了向前 ...

  9. babel的安装和使用方法

    要使用Babel, 我们需要nodeJS的环境和npm, 主要安装了nodeJS, npm就默认安装了 , 现在安装nodeJS很简单了, 直接下载安装就好了: 安装es-checker 在使用Bab ...

  10. hihoCoder 1584 Bounce 【数学规律】 (ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛)

    #1584 : Bounce 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 For Argo, it is very interesting watching a cir ...