UIAlertController简单封装和不定参数

我们在使用UIAlertController时一般会这样

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Alert" message:@"这是一个Alert" preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction *alertAction0 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
       
    }];
UIAlertAction *alertAction1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
       
    }];

[alert addAction:alertAction0];
[alert addAction:alertAction1];
//或者用for循环添加Action

这样写一次两次还好,如果要多次用到Alert,每次都要重复一次这一堆。于是就简单地封装了一下,把这一堆放进一个方法中。
像这样:

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Alert" message:@"这是一个简单封装的Alert" preferredStyle:UIAlertControllerStyleAlert handler:^(UIAlertAction *action) {
        if ([action.title isEqualToString:@"取消"]) {
            NSLog(@"点击了>>>>取消");
        }
        if ([action.title isEqualToString:@"确定"]) {
            NSLog(@"点击了>>>>确定");
        }
        if ([action.title isEqualToString:@"ok"]) {
            NSLog(@"点击了>>>>ok");
        }
    } cancelButtonTitle:@"取消" otherButtonTitles:@"确定",@"ok", nil];
   // [self presentViewController:alert animated:YES completion:nil];

这样感觉清爽不少_
这里用到了不定参数,关于其使用我在方法的实现中已经注释,有兴趣的同学可以去下载demo查看。

时间仓促,只是简单封装,有误之处还请指正。

这一篇文章介绍iOS链式编程iOS,Objective-C链式编程简谈

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,259评论 4 61
  • 1 老屋是爷爷亲手建起来的。 我出生的时候,老屋里有六个人:爷爷,奶奶,父亲,母亲,叔叔,我。 两年后,叔叔结婚了...
    七十七号阅读 481评论 0 5
  • 礼物是满满一本本的画。 上一篇有人祝我告白成功。 可是已经分手了啊。 怎么办。
    MantlePatient阅读 366评论 4 14
  • 我总是凌晨两点回家, 凌晨三点睡觉。 为什么?
    饭团人阅读 283评论 0 0
  • 《冬雪情韵》 文/梦飞梨花 望着窗外的雪花纷飞 我不清楚那一刻的心情 只记得雪落满了整个世界 保暖衣裹紧的躯干仍然...
    梦飞梨花不聊天阅读 297评论 0 1