iOS 尝试用 block 闭包 去代替delegate 实现方法
1 | 通常都是这样创建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];
posted on 2015-12-04 17:01 ACM_Someone like you 阅读(478) 评论(0) 编辑 收藏 举报
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
· 上周热点回顾(2.17-2.23)