KDotAlert
一个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的更多相关文章
随机推荐
- 如何快速的开发一个完整的iOS直播app(美颜篇)
前言 在看这篇之前,如果您还不了解直播原理,请查看这篇文章如何快速的开发一个完整的iOS直播app(原理篇) 开发一款直播app,美颜功能是很重要的,如果没有美颜功能,可能分分钟钟掉粉千万,本篇主要讲 ...
- Mecanim动画编辑器 - 加入动画层实现并行动作
1.创建新的状态层 a) 通过下图的1button创建一个新的层 b) 通过下图2属性设置图层的权重.假设为0,则该图层的状态不会影响到总的状态机 c) Mask是设置动画的Avatar的关联节 ...
- JS创建对象几种不同方法具体解释
1.工厂模式 弊端:没有解决对象的识别问题,即怎么知道一个对象的类型. 2.构造函数模式 与工厂模式相比: 1.没有显式的创建对象 2.直接将属性和方法赋给了this对象 3.没有return语句 要 ...
- STL algorithm算法max,max_elements(33)
max原型: std::max C++98 C++11 C++14 default (1) template <class T> const T& max (const T& ...
- C++写动态站点之HelloWorld!
演示样例源码下载地址:Fetch_Platform.7z 更复杂的代码可參考本博客BBS的实现 简单的说.动态站点就是能够动态变更的站点.动态变化的内容通常来自后端数据库.例如以下省略万字(动态站点) ...
- sqlite学习笔记10:C语言中使用sqlite之查询和更新数据
前面说到的 sqlite_exec() 中的第三个參数, SQLite 将为 sql 參数内运行的每一个 SELECT 语句中处理的每一个记录调用这个回调函数. 本节加入了两个函数.selectFro ...
- AOP和OOP的区别
AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术. AOP与OOP是面向不同领域的两种设计思想. ...
- 利用easyUI的combobox打造自己主动提示组件
自己主动提示是时下一个非常流行的功能,比方说百度.谷歌的搜索输入框都使用到了这么一个功能. 因为easyUI的combobox设计师已经考虑到了这个功能.所以仅仅需简单几步我们能够轻松打造自己的自己主 ...
- python 爬虫1 開始,先拿新浪微博開始
刚刚開始学. 目的地是两个.一个微博,一个贴吧 存入的话,临时还没想那么多.先存到本地目录吧 分词和推荐后面在整合 mysql mongodb hadoop redius 后面在用 我最终知道为什么大 ...
- SimpleHTTPServer
SimpleHTTPServer python -m SimpleHTTPServer 8989