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 = ;
btn.layer.masksToBounds = YES;
btn.layer.borderColor = COLOR_THEME_GREEN.CGColor;
btn.layer.borderWidth = ;
[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:@"" action:^{
NSLog(@"");
}],[HFLittleHelperButton buttonWithNormalTitle:@"" action:^{
NSLog(@"");
}],[HFLittleHelperButton buttonWithNormalTitle:@"" action:^{
NSLog(@"");
}],nil];
[alert showHelperAlertViewView];

iOS 尝试用 block 闭包 去代替delegate 实现方法的更多相关文章
- 【记录】尝试用QEMU模拟ARM开发板去加载并运行Uboot,kernel,rootfs【转】
转自:https://www.crifan.com/try_use_qemu_emulate_arm_board_to_load_and_run_uboot_kernel_rootfs/ [背景] 手 ...
- 【记录】尝试用android-logging-log4j去实现log输出内容到sd卡中的文件的功能
[背景] 折腾: [记录]给Android中添加log日志输出到文件 期间,已经试了: [记录]尝试用android中microlog4android实现log输出到文件的功能 但是不好用. 然后就是 ...
- 【转】iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault/单例)-- 不错
原文网址:http://www.cnblogs.com/JuneWang/p/3850859.html iOS页面间传值的方式(NSUserDefault/Delegate/NSNotificatio ...
- iOS页面间传值的方式 (Delegate/NSNotification/Block/NSUserDefault/单例)
iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault/单例) iOS页面间传值的方式(NSUserDefault/Delegate/NSN ...
- 【ios开发】Block编程
1 什么是block iOS SDK 4.0开始,Apple引入了block这一特性.字面上说,block就是一个代码块,但是它的神奇之处在于在内联(inline)执行的时候(这和C++很像)还可以传 ...
- 【ios】使用Block对POST异步操作的简单封装
以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/3409721.html 一般情况下的POST异步操作需要实现以下 ...
- iOS 开发之Block
iOS 开发之Block 一:什么是Block.Block的作用 UI开发和网络常见功能的实现回调,按钮事件的处理方法是回调方法. 1. 按钮事件 target action 机制. 它是将一 ...
- 李洪强iOS开发之Block和协议
李洪强iOS开发之Block和协议 OC语言BLOCK和协议 一.BOLCK (一)简介 BLOCK是什么?苹果推荐的类型,效率高,在运行中保存代码.用来封装和保存代码,有点像函数,BLOCK可以在任 ...
- 尝试用React写几个通用组件 - 带搜索功能的下拉列表,开关切换按钮,弹出框
尝试用React写几个通用组件 - 带搜索功能的下拉列表,开关切换按钮,弹出框 近期正在逐步摸索学习React的用法,尝试着写几个通用型的组件,整体项目还是根据webpack+react+css-me ...
随机推荐
- UnrealEngine4 尝鲜
把官方事例的demo扩展成横版过关视角
- C#实现发送手机短信
- Eclipse更改默认工作环境编码为UTF-8(9.6)
1 window---->preference------>General----->Workspace---->Text file encoding---->Other ...
- LINUX find 实用
查找文件find ./ -type f 查找目录find ./ -type d 查找名字为test的文件或目录find ./ -name test 查找名字符合正则表达式的文件,注意前面的‘.*’(查 ...
- python截取搜索引擎关键词
这段代码是自己学了python的基本语法之后,参考一个网上视频写的代码,功能是截取搜索引擎360的关键词. 代码: #!/usr/bin/python #encoding:utf-8 import u ...
- [译]GLUT教程 - 位图字体
Lighthouse3d.com >> GLUT Tutorial >> Fonts >> Bitmap Fonts 位图字体一般是二维字体.虽然我们会把它放到三维 ...
- 【Atheros】pktgen的ipv6地址处理函数参考及ipv6基本知识
pktgen有很多函数可以作为很好的网络相关的工具函数,这里列出ipv6中1:0:0:0:0:0:0:1和1::1这两种地址形式相互转化的工具函数. 第一个函数,用于把一个1:0:0:0:0:0:0: ...
- ASP.NET动态网站制作(21)-- C#(4)
前言:这节课是C#讲解的第四节课,主要围绕面向对象的三大特性展开.上节课已经把封装讲完了,这节课讲继承和多态. 内容: 1.继承:写程序的时候有些信息是公共的,可以将这些公共的信息写在父类里,增强代码 ...
- 自定义cginc文件
首先定义一个cginc文件如下所示: #ifndef MY_CG_INCLUDE #define MY_CG_INCLUDE struct appdata_x { float4 vertex : PO ...
- pdf文件编辑
下载软件:Foxit PDF Editor,这个工具挺好用的,可以对pdf文件内容进行编辑 Foxit PDF Editor 是第一个真正的PDF文件编辑软件.许多人都希望能找到一个象编辑其它类型的文 ...