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];

 

iOS 尝试用 block 闭包 去代替delegate 实现方法_第1张图片




转载于:https://www.cnblogs.com/someonelikeyou/p/5019726.html

你可能感兴趣的:(iOS 尝试用 block 闭包 去代替delegate 实现方法)