有block回调的UIButton和Alert
来源:互联网 发布:装修论坛 淘宝 编辑:程序博客网 时间:2024/04/30 21:23
实现方式就是自定义一个继承于UIButton或者UIAlert的类,然后添加一个block的属性。
重写UIButton的初始化方法,
- (id)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { [self addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside]; } return self;}- (void)onClick:(id)sender{ _block(self);}
有block的alert,代码如下:
同样初始化init方法- (id)initWithTitle:(NSString *)title message:(NSString *)message cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString*)otherButtonTitles block:(TouchBlock)block{ self = [super initWithTitle:title message:message delegate:self cancelButtonTitle:cancelButtonTitle otherButtonTitles:otherButtonTitles, nil];//注意这里初始化父类的 if (self) { self.block = block; } return self;}//#pragma mark -AlertViewDelegate- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ //这里调用函数指针_block(要传进来的参数); _block(buttonIndex);}
示例代码:
BlockButton *button = [[BlockButton alloc]initWithFrame:CGRectMake(100, 100, 40, 20)]; [button setBlock:^(BlockButton *button){ AlertBlock *alert = [[AlertBlock alloc]initWithTitle:@"提示" message:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定" block:^(NSInteger buttonIndex){ //在这里面执行触发的行为,省掉了代理,这样的好处是在使用多个Alert的时候可以明确定义各自触发的行为,不需要在代理方法里判断是哪个Alert了 if (buttonIndex == 0) { NSLog(@"取消"); }else if (buttonIndex == 1){ NSLog(@"确定"); } }]; [alert show]; }];
0 0
- 有block回调的UIButton和Alert
- UITableViewCell里UIButton实现push的代理和block
- UITableViewCell里UIButton实现push的代理和block
- iOS中UIButton的block
- UIButton+Block
- uibutton block
- 将UIButton的SEL转成block
- 让你的UIButton自带block
- Alert Block方法.
- block回调UIButton
- block自定义UIButton
- 二十二, iOS UIButton的Block的使用
- UIButton.titleLabel.text和[UIButton setTitle:],UIButton.imageView.image和[UIButton setImage]的区别
- iOS开发利用UIButton和UIImageView的区别分辨哪些控件有addTarget监听事件这个方法
- UIButton的点击事件写成Block,AlertView的delegate写成Block
- UIImageView和UIButton的区别
- UIImageView和UIButton的区别
- UILabel和UIButton的属性
- Eclipse打包时出现export aborted because fatal lint errors
- Hibernate配置文件与映射文件详解
- windows server 2008防火墙阻止局域网不能访问解决方案
- NTFS和FAT
- cookie 和session 的区别详解
- 有block回调的UIButton和Alert
- triangle of success
- 虚拟机安装xp系统
- iOS的socket开发基础
- 增量备份、差异备份和完全备份的区别
- Wamp环境下,手机端访问localhost
- C语言输入一个整数,输出其二进制位中1的个数
- Eclipse使用AmaterasUML
- 工厂模式(1)