基于UIAlertController的封装调用

UIAlertController是iOS8之后推出的合并UIAlert和UIActionSheet的控件,我相信很多地方都会使用到的,因此在一个单例中对其进行封装一下,每次使用时不必再写那么重复的代码,如下:

单例.h文件中

#pragma mark 显示ios默认样式的confrim框(基于UIAlertController)
/**
 *  @author zm
 *
 *  @brief 显示ios默认样式的confrim框(基于UIAlertController)
 *
 *  @param title          标题
 *  @param message        内容
 *  @param confirmHandler confirm的block
 *  @param confirmTitle   confirm的标题
 *  @param cancelHandler  cancel的block
 *  @param cancelTitle    cancel的标题
 *  @param target         加载对象
 */
- (void)f_showConfirm_default_title:(NSString *)title message:(NSString *)message confirmHandler:(void (^)(UIAlertAction *action))confirmHandler  confirmTitle:(NSString *)confirmTitle cancelHandler:(void (^)(UIAlertAction *action))cancelHandler  cancelTitle:(NSString *)cancelTitle target:(UIViewController *)target;

单例.m文件中

#pragma mark 显示ios默认样式的confrim框(基于UIAlertController)
- (void)f_showConfirm_default_title:(NSString *)title message:(NSString *)message confirmHandler:(void (^)(UIAlertAction *action))confirmHandler  confirmTitle:(NSString *)confirmTitle cancelHandler:(void (^)(UIAlertAction *action))cancelHandler  cancelTitle:(NSString *)cancelTitle target:(UIViewController *)target
{
    UIAlertController *_alertC_temp = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *_alertAct_cancel = [UIAlertAction actionWithTitle:cancelTitle style:UIAlertActionStyleCancel handler:cancelHandler];
    [_alertC_temp addAction:_alertAct_cancel];
    UIAlertAction *_alertAct_confirm = [UIAlertAction actionWithTitle:confirmTitle style:UIAlertActionStyleDefault handler:confirmHandler];
    [_alertC_temp addAction:_alertAct_confirm];
    [target presentViewController:_alertC_temp animated:YES completion:nil];
}

调用时

__weak __typeof(self) weakSelf = self;
    void(^confirmHandler)(UIAlertAction *action) = ^(UIAlertAction *action) {
        [[weakSelf 方法];
    };
    void(^cancelHandler)(UIAlertAction *action) = ^(UIAlertAction *action) {
        XNLog(@"我是cancel回调");
    };
    [[xn_base_Handler_Alert_HUD sharedInstance] f_showConfirm_default_title:@"" message:@"确定取消订单吗?" confirmHandler:confirmHandler confirmTitle:@"确定" cancelHandler:cancelHandler cancelTitle:@"取消" target:self];
这是对一个最基本的含有@“确定”、@“取消”按钮进行的简单封装,如想添加text等或只要确定、取消其中的一个按钮,均可在此基础上进行修改。


  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值