zoukankan      html  css  js  c++  java
  • iOS 尝试用 block 闭包 去代替delegate 实现方法

    通常都是这样创建alert 再加一个代理
    // 创建一个UIAlertView并显示出来
    UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:aTitle message:msg delegate:self cancelButtonTitle:str otherButtonTitles:nil];
    [alertview show];
    
    -(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
        NSLogD(@"%ld", (long)buttonIndex);
    }
    如果 像下边的写法 就显得牛逼多了
    [[[UIAlertView alloc] initWithTitle:@"Delete This Item?"
                                message:@"Are you sure you want to delete this really important thing?"
                       cancelButtonItem:[RIButtonItem itemWithLabel:@"Yes" action:^{
                                          // Handle "Cancel"
                                        }]
                       otherButtonItems:[RIButtonItem itemWithLabel:@"Delete" action:^{
                                           // Handle "Delete"
                                        }], nil] show];
    这个 就是 闭包强大的地方了 不需要写代理了哈哈 多方便 可读性还强 

    得了懒癌 就去这个链接 下载直接用 不然 就往下看哦 高度自定义 https://github.com/jivadevoe/UIAlertView-Blocks

    就是在 想要执行 这个 block方法的时候 实施方法 xxx.action();当然 要预判断 是否存在这个方法 if(xxx.action) //这是个无参数的 闭包 如果要写有参数 闭包 参见 闭包的写法

    //
    //  HFLittleHelperButton.h
    //  dailylife
    //
    //  Created by HF on 15/12/5.
    //
    //
    
    #import "HFBorderLineButton.h"
    
    @interface HFLittleHelperButton : HFBorderLineButton
    
    @property (copy, nonatomic) void (^action)();
    
    + (id)buttonWithNormalTitle:(NSString *)title action:(void(^)(void))action;
    @end
    关键方法已做标注 了 重点 应该看如何调用的 这个.action方法  也可以在需要的地方获取 这个 实施的btn对象 在需要到的地方  然后触发.action()都可以
    //
    // HFLittleHelperButton.m // dailylife // // Created by HF on 15/12/5. // // #import "HFLittleHelperButton.h" @implementation HFLittleHelperButton + (id)buttonWithNormalTitle:(NSString *)title action:(void(^)(void))action { HFLittleHelperButton *btn = [HFLittleHelperButton buttonWithType:UIButtonTypeCustom]; [btn setTitle:title forState:UIControlStateNormal]; [btn setTitleColor:COLOR_THEME_GREEN forState:UIControlStateNormal]; [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted]; [btn setBackgroundImage:[HuofarUtils createImageWithColor: COLOR_THEME_GREEN] forState:UIControlStateHighlighted]; btn.backgroundCustomColor = COLOR_THEME_GREEN; btn.layer.cornerRadius = 2; btn.layer.masksToBounds = YES; btn.layer.borderColor = COLOR_THEME_GREEN.CGColor; btn.layer.borderWidth = 1; [btn setAction:action];

          [btn addTarget:self action:@selector(btnBlockAction:) forControlEvents:UIControlEventTouchUpInside];

        return btn;
    } 

    - (void)btnBlockAction:(HFLittleHelperButton *)btn{

        if(btn.action)

            btn.action();

    }

    @end

    实战:
    HFLittleHelperAlertView *alert = [[HFLittleHelperAlertView alloc]initWithBlockExpression:HelperAlertViewExpressionTypePlease Title:@"标题名称" message:@"好好学习天天向上好好学习天天向上好好学习天天向上好好学习天天向上好好学习天天向上好好学习天天向上" withView:nil cancelButton:[HFLittleHelperButton buttonWithCancelTitle:@"取消" action:^{
     NSLog(@"XXXXXX");
     }] otherButtons:[HFLittleHelperButton buttonWithNormalTitle:@"111111" action:^{
     NSLog(@"111111");
     }],[HFLittleHelperButton buttonWithNormalTitle:@"222222" action:^{
     NSLog(@"222222");
     }],[HFLittleHelperButton buttonWithNormalTitle:@"333333" action:^{
     NSLog(@"333333");
     }],nil];
     [alert showHelperAlertViewView];




  • 相关阅读:
    PDA设备小知识--(IP)工业防护等级含义
    实施项目--如何推进项目实施进度
    Git.Framework 框架随手记--存储过程简化
    Git.Framework 框架随手记--SQL配置文件的使用
    Git.Framework 框架随手记--ORM查询数据集合 二
    介绍 Scratch 3.0:扩展编码创造力
    微服务架构:引领数字化转型的基石
    网易云技术开放日 云安全专场分享圆满结束
    JVM调优推荐
    springboot + mybatis + 多数据源
  • 原文地址:https://www.cnblogs.com/someonelikeyou/p/5019726.html
Copyright © 2011-2022 走看看