block使用的一个例子
2015-11-19 11:39
288 查看
.h
// Copyright © 2015年 1. All rights reserved.
//
@end
@implementation BlockDemoViewController
(void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
BlockCustomItem *customItem = [[BlockCustomItem alloc] init];
customItem.block = ^{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@”提示” delegate:self cancelButtonTitle:@”取消” otherButtonTitles:@”确定”, nil];
[alert show];
};
BlockCustomButton *blockButton = [BlockCustomButton buttonWithType:UIButtonTypeCustom];
blockButton.frame = CGRectMake(50, 100, 100, 30);
blockButton.backgroundColor = [UIColor colorWithRed:59.0/255.0 green:249.0/255.0 blue:199.0/255.0 alpha:1.0];
blockButton.item = customItem;
[blockButton addTarget:self action:@selector(blockButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:blockButton];
}
(void)blockButtonAction:(BlockCustomButton *)button {
if (button.item.block) {
button.item.block();
}
}
(void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
.h
@property (nonatomic, strong) BlockCustomItem *item;
@end
.m
@end
.h
(id)init {
self = [super init];
if (self) {
self.title = @”“;
}
return self;
}
@end
// Copyright © 2015年 1. All rights reserved.
//
import
import “BlockDemoViewController.h”
import “BlockCustomItem.h”
import “BlockCustomButton.h”
@interface BlockDemoViewController ()@end
@implementation BlockDemoViewController
(void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
BlockCustomItem *customItem = [[BlockCustomItem alloc] init];
customItem.block = ^{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@”提示” delegate:self cancelButtonTitle:@”取消” otherButtonTitles:@”确定”, nil];
[alert show];
};
BlockCustomButton *blockButton = [BlockCustomButton buttonWithType:UIButtonTypeCustom];
blockButton.frame = CGRectMake(50, 100, 100, 30);
blockButton.backgroundColor = [UIColor colorWithRed:59.0/255.0 green:249.0/255.0 blue:199.0/255.0 alpha:1.0];
blockButton.item = customItem;
[blockButton addTarget:self action:@selector(blockButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:blockButton];
}
(void)blockButtonAction:(BlockCustomButton *)button {
if (button.item.block) {
button.item.block();
}
}
(void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
.h
import
import “BlockCustomItem.h”
@interface BlockCustomButton : UIButton@property (nonatomic, strong) BlockCustomItem *item;
@end
.m
import “BlockCustomButton.h”
@implementation BlockCustomButton@end
.h
import
import “BlockCustomItem.h”
@implementation BlockCustomItem(id)init {
self = [super init];
if (self) {
self.title = @”“;
}
return self;
}
@end
相关文章推荐
- JSP内置对象
- 深入理解java语言的class文件格式(三)
- PHP新建类问题分析及解决思路
- Android搜索功能实现
- java变参
- zend studio的安装和破解,并安装相应的emmet插件
- xshell 常用的使用方法
- C语言结构体指针在函数调用中的使用
- 深入理解java语言的class文件格式(二)
- Android Webview 使用小结
- 关于des文件的加密,解密。
- 文章标题
- MTK cmdline传递过程
- Android 最火的快速开发框架XUtils
- 操作系统Unix、Windows、Mac OS、Linux的故事
- linux usb枚举过程分析之守护进程及其唤醒【转】
- 枚举操作笔记
- 多层下拉选择框的实现。
- 【android】开发中遇到的一些问题
- 创业者们还有成为下一个BAT级存在的机会