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

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

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

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

  1. NS_CLASS_AVAILABLE_IOS(8_0) @interface UIAlertController : UIViewController
  2.  
  3. + (instancetype)alertControllerWithTitle:(nullable NSString *)title message:(nullable NSString *)message preferredStyle:(UIAlertControllerStyle)preferredStyle;
  4.  
  5. - (void)addAction:(UIAlertAction *)action;
  6. @property (nonatomic, readonly) NSArray<UIAlertAction *> *actions;
  7.  
  8. @property (nonatomic, strong, nullable) UIAlertAction *preferredAction NS_AVAILABLE_IOS(9_0);
  9.  
  10. - (void)addTextFieldWithConfigurationHandler:(void (^ __nullable)(UITextField *textField))configurationHandler;
  11. @property (nullable, nonatomic, readonly) NSArray<UITextField *> *textFields;
  12.  
  13. @property (nullable, nonatomic, copy) NSString *title;
  14. @property (nullable, nonatomic, copy) NSString *message;
  15.  
  16. @property (nonatomic, readonly) UIAlertControllerStyle preferredStyle;
  17.  
  18. @end

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

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

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

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

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

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

看下定义:

  1. NS_CLASS_AVAILABLE_IOS(8_0) @interface KDotAlert : NSObject
  2.  
  3. @property (nonatomic, strong, readonly) KDotAlertFormat format; // format title & message
  4. @property (nonatomic, strong, readonly) NSString * _Nullable title; // title
  5. @property (nonatomic, strong, readonly) NSString * _Nullable message; // message
  6. @property (nonatomic, assign, readonly) UIAlertControllerStyle preferredStyle; // The style of the alert controller.
  7.  
  8. @property (nonatomic, strong, readonly) KDotAlertAction action; // Attaches an action object to the alert or action sheet. default style;
  9. @property (nonatomic, strong, readonly) KDotAlertAction cancel; // ... cancel style;
  10. @property (nonatomic, strong, readonly) KDotAlertAction destructive;// ... destructive style;
  11.  
  12. // The actions that the user can take in response to the alert or action sheet.
  13. @property (nonatomic, strong, readonly) NSArray<UIAlertAction *> * _Nullable actions;
  14.  
  15. // Make the preferred action for the user to take from an alert.
  16. @property (nonatomic, strong, readonly) KDotAlertPreferred preferred NS_AVAILABLE_IOS(9_0);
  17.  
  18. @property (nonatomic, strong, readonly) KDotAlertTextField textField; // Adds a text field to an alert;
  19. @property (nonatomic, strong, readonly) NSArray<UITextField *> * _Nullable textFields; // The array of text fields displayed by the alert.
  20.  
  21. @property (nonatomic, strong, readonly) KDotAlertShow show; // show with viewController
  22.  
  23. + (instancetype _Nonnull )alert;
  24. + (instancetype _Nonnull )actionSheet;
  25.  
  26. @end

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

KDotAlert的更多相关文章

随机推荐

  1. Tar压缩文件

    [root@test /root]# tar [-zxcvfpP] filename   [root@test /root]# tar -N 'yyyy/mm/dd' /path -zcvf targ ...

  2. python的分布式队列神器 Celery

    pip search kafka可以搜索到很多kafka的python插件,选择一个大家用的比较多的 celery结合kafka使用 使用kafka扩展python logging集中式日志收集 re ...

  3. BUPT复试专题—最近公共祖先(2014软院)

    题目描述 给出一棵有N个节点的有根树TREE(根的编号为1),对于每组查询,请输出树上节点u和v的最近公共祖先. 最近公共祖先:对于有向树TREE的两个结点u,v.最近公共祖先LCA(TREE u,v ...

  4. FastDFS的配置、部署与API使用解读(3)以流的方式上传文件的客户端代码(转)

    调用的API为: String[] upload_file( String group_name,//组名,不指定则可设为null long file_size,//文件大小,必须制定 UploadC ...

  5. 项目记录26--unity-tolua框架 View03-UIManager.lua

    做为程序员要懂得假设保持健康,对电脑时间太长非常easy眼花,得脖子病,腰都疼,这星期六日组团到康宁去了,哈哈. 一个字"疼"!!!! 废话不多少,把UIManager.lua个搞 ...

  6. [WF4.0 实战] 事件驱动应用

    看到题目或许非常多人都会疑问,为什么要使用事件监听呢? 眼下的认识: 1,使用事件监听能够将工作流的结点返回值返回到client 2,能够实现等待与重新启动,相当于之前的WaitActivity创建B ...

  7. ios如何获取手机的网络状态和运营商名称

    本文转载至 http://blog.csdn.net/justinjing0612/article/details/38313747 以前获取手机的网络状态和运营商名称都是似有API, 现在我们可以大 ...

  8. 【bzoj1433】[ZJOI2009]假期的宿舍

    按要求连边,跑匈牙利 #include<algorithm> #include<iostream> #include<cstdlib> #include<cs ...

  9. 移动web开发适配rem

    移动的meta标签 <meta  name="viewport" content="width=device-width, initial-scale=1,user ...

  10. 解决Javascript md5 和 Java md5 中文加密后不同问题

    Javascript md5 和 Java md5 带中文字符加密结果不一致,可以通过编码进行转化. javascript可以使用encodeURLComponent将中文先转化一次再进行MD5加密. ...