简单封装UIAlertController 方便使用

今天看到下面一段代码 ,感觉这代码写的好累🤣🤣🤣。然后就简单的封装一下UIAlertController以方便使用,仅仅只有一个action和两个action的。🤔🤔🤔

原始代码截图

修改后代码如下,是不是看着舒服多了🤓🤓🤓

修改后代码截图

我是给UIViewController添加了一个category,这样只要是UIViewController里都可以直接调用。

代码如下:

#import "UIViewController+AlertVC.h"

@implementation UIViewController (AlertVC)

- (void)presentOneActionAlertControllerTitle:(NSString *_Nullable)aTitle message:(NSString *_Nullable)aMessage actionTitle:(NSString *_Nullable)actionTitle handler:(void (^ _Nullable )(UIAlertAction * _Nullable action))handler {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:aTitle message:aMessage preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *leftAction  = [UIAlertAction actionWithTitle:actionTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        handler(action);
    }];
    [alert addAction:leftAction];
    [self presentViewController:alert animated:YES completion:nil];
}

- (void)presentTwoActionAlertControllerTitle:(NSString *_Nullable)aTitle message:(NSString *_Nullable)aMessage rightTitle:(NSString *_Nullable)rightTitle leftTitle:(NSString *_Nullable)leftTitle rightActionStyle:(UIAlertActionStyle)style rightHandler:(void (^ _Nullable )(UIAlertAction * _Nullable action))rightHandler leftHandler:(void (^ _Nullable )(UIAlertAction * _Nullable action))leftHandler {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:aTitle message:aMessage preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *rightAction  =  [UIAlertAction actionWithTitle:rightTitle style:style handler:^(UIAlertAction * _Nonnull action) {
        rightHandler(action);
    }];
    UIAlertAction *leftAction  = [UIAlertAction actionWithTitle:leftTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        leftHandler(action);
    }];
    [alert addAction:rightAction];
    [alert addAction:leftAction];
    [self presentViewController:alert animated:YES completion:nil];
}

大家一起愉快的Alert弹框吧😉😉😉

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

友情链接更多精彩内容