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的更多相关文章
随机推荐
- Service具体解释(一):什么是Service
< Service具体解释(一):什么是Service> < Service具体解释(二):Service生命周期> <Service具体解释(三):Service的使用 ...
- C语言里字符串的解析
根据给定的字符串,按照一定规则解析字符串,卡住好几次,这次做个笔记,以供参考 函数名称: strtok 函数原型: char *strtok(char *s1, const char *s ...
- angularJS---自己定义过滤器
AngularJS还有一个特点就是提供了过滤器.能够通过操作UNIX下管道的方式,操作数据结果. 通过使用管道.能够便于双向的数据绑定中视图的展现. 过滤器在处理过程中,将数据变成新的格式.并且能够使 ...
- aip接口中对url参数md5加密防篡改的原理
目前网上所有开放api的网站中,数据的调用都是采用同一种方式,即: http:www.xxx.com/aa=1&bb=2...,原后对这些参数按字典顺序排序后进行md5加密,将md5加密串与接 ...
- centos笔记-安装特定版本的mysql
centos6的yum默认安装的mysql是5.1版, 如果要安装5.6.16 版,有三个办法 1.yum方式, 这个方式的好处是通过yum安装卸载都很方便,坏处是版本无法详细制定,比如官方版本yum ...
- debian iptables持久化
1 保存iptables iptables-save > /etc/iptables.rules 2 创建启动文件 touch /etc/network/if-pre-up.d/iptabl ...
- Android Studio解决导入项目非常慢
Android Studio比Eclipse ADT有巨大的优势.Android Studio原生支持使用Gradle来构建项目,使用动态语言Groovy定义项目构建的过程,避免了build.xml文 ...
- 从小姐姐博客那里看到的流光文字(CSS3 animate)
对于流光文字,大家并不陌生,毕竟我们都经历过非主流的时代.你们卟懂绯紸流!色彩缤纷的QQ空间...... 还记得那些炫酷的签名档,或者那些炫酷的动态头像.不过大家对于流光文字的印象还是图片.那么在网页 ...
- iOS UI控件之间的关系图
- spring的依赖注入(DI)、控制反转(IOC)和面向切面(AOP)
在spring的配置文件增加 <context:component-scan base-package="com.jmu.ccjoin.service"/> <c ...