block自定义UIButton
2016-07-04 21:31
501 查看
ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
MyButton *button = [MyButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(20, 50, 280, 50);
button.backgroundColor = [UIColor redColor];
[self.view addSubview:button];
//实现button的block回调
[button setButtonShouldBlock:^(MyButton *sender) {
NSLog(@"我是Block: should方法\n");
}];
[button setButtonWillBlock:^(MyButton *sender) {
NSLog(@"我是Block: Will方法\n");
}];
[button setButtonDidBlock:^(MyButton *sender) {
NSLog(@"我是Blcok: Did方法\n");
}];
}
@interface MyButton :
UIButton
- (void)viewDidLoad
{
[super viewDidLoad];
MyButton *button = [MyButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(20, 50, 280, 50);
button.backgroundColor = [UIColor redColor];
[self.view addSubview:button];
//实现button的block回调
[button setButtonShouldBlock:^(MyButton *sender) {
NSLog(@"我是Block: should方法\n");
}];
[button setButtonWillBlock:^(MyButton *sender) {
NSLog(@"我是Block: Will方法\n");
}];
[button setButtonDidBlock:^(MyButton *sender) {
NSLog(@"我是Blcok: Did方法\n");
}];
}
@interface MyButton :
UIButton
typedef void (^ButtonWillBlock)(MyButton *sender); typedef void (^ButtonDidBlock)(MyButton *sender); typedef void (^ButtonShouldBlock)(MyButton *sender); @property (nonatomic, copy) ButtonWillBlock willBlock; @property (nonatomic, copy) ButtonDidBlock didBlock; @property (nonatomic, copy) ButtonShouldBlock shouldBlock; //接受block的方法 -(void)setButtonShouldBlock: (ButtonShouldBlock) block; -(void)setButtonWillBlock: (ButtonWillBlock) block; -(void)setButtonDidBlock:(ButtonDidBlock) block;
//接受block块 -(void)setButtonShouldBlock: (ButtonShouldBlock) block { self.shouldBlock = block; [self addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside]; } -(void)setButtonWillBlock: (ButtonWillBlock) block { self.willBlock = block; } -(void)setButtonDidBlock:(ButtonDidBlock) block { self.didBlock = block; } - (void)buttonClick { if (_shouldBlock) { _shouldBlock(self); NSLog(@"调用了"); self.backgroundColor = [UIColor grayColor]; } }
相关文章推荐
- VM虚拟机----安装虚拟机时出现failed to create the requested registry key
- UIPopoverController
- TOJ 1765. Longest Ordered Subsequence
- java 获取request参数集
- 使用UISearchController的时候 cellForRow 崩溃
- 百度富文本编辑器UEDITOR
- QtQuick和PHP 最简单的通信demo
- Android 高级UI设计笔记19:PopupWindow使用详解
- W3AF FuzzableRequest 处理流程札记
- W3AF FuzzableRequest 处理流程札记
- kcachegrind gui for callgrind
- Gradle version 2.2 is required. Current version is 2.10.
- [uoj#209][UER#6A]票数统计
- [ST表] Codeforces Abbyy Cup 2.0-Final B Sequence
- 【Poj】-3061-Subsequence
- [状压DP] Codeforces Beta Round #72 Div1 E Two Subsequences
- 子线程更新UI方式
- UI碎片控件之Fragment——底部导航栏的实现(方法1)
- iOS 【UIKit-设置navigationBar统一样式几点技巧】
- UI碎片控件之Fragment基本概述