一个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. Mecanim动画编辑器 - 加入动画层实现并行动作

    1.创建新的状态层 a) 通过下图的1button创建一个新的层   b) 通过下图2属性设置图层的权重.假设为0,则该图层的状态不会影响到总的状态机  c) Mask是设置动画的Avatar的关联节 ...

  2. 关于HTML中文乱码问题

    系统:ubuntu 14.04 软件:bluefish 一.乱码原因 1.不同编码内容混杂:HTML乱码是因为html编码问题照成(常见gb2312与utf-8两种编码内容同一时候存在照成) 2.未设 ...

  3. 适合新人学习的iOS官方Demo

    UICatalog.包括了绝大部分经常使用的UI,入门必备良药. 9  分段选择器 10滑动条 Slider 11stack view 12 分步条 13 开关 14 textfield 15text ...

  4. three.js 源代码凝视(十五)Math/Plane.js

    商域无疆 (http://blog.csdn.net/omni360/) 本文遵循"署名-非商业用途-保持一致"创作公用协议 转载请保留此句:商域无疆 -  本博客专注于 敏捷开发 ...

  5. C语言之基本算法34—分解质因数(方法一)

    //矩阵基础 /* ================================================================== 题目:输入一个正整数.将其分解为质因式,如:6 ...

  6. Java经常使用类及其经常用法

    1.ArrayList java.util.ArrayList<E> add(E e) //插入尾部 add(int index, E element) remove(int index) ...

  7. 让你的eclipse实现写JAVA代码,HTML,CSS,JAVASCRIPT代码提示

    1.打开eclipse→Windows→Preferences→Java→Editor→Content Assist 改动Auto Activation triggers for java的值为:zj ...

  8. oracle user locke

    1:管理员登录 sqlplus sys/pwd as sysdba sql->alter user jd account unlock; commit; SQL> password new ...

  9. python day-15 匿名函数 sorted ()函数 filter()函数 map()函数 递归 二分法

    一.匿名函数 匿名函数的结构:变量   =  lamda  参数: 返回值 a  =  lamda  x : x*x       # x为参数,   : 后边的为函数体 print(a(x)) def ...

  10. java的gradle项目的基本配置

    plugins { id 'org.springframework.boot' version '2.1.4.RELEASE' id 'java' } apply plugin: 'io.spring ...