#import "ViewController.h"
//定义一个block
typedef void (^block)(int, int);
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//定义一个block
block myBlock = ^(int a, int b){
NSLog(@"%i, %i", a, b);
};
//调用 [self testBlock:myBlock];
}
- (void)testBlock:(block)myBlock{
//回调 myBlock(10, 30);
}
@end
eg2#import "ViewController.h"#import "BlockButton.h"#import "DetailViewController.h"typedef void (^block)(int, int);@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad{ [super viewDidLoad];// Do any additional setup after loading the view, typically from a nib. block myBlock = ^(int a, int b){ NSLog(@"%i, %i", a, b); }; [self testBlock:myBlock]; //block引用局部变量 int number = 100; __block int number2 = 200; block myblock2 = ^(int a, int b){ //访问okay, 修改不可,被当作常量 NSLog(@"%i, %i", a, b + number); // number 还是100, 不会改变。 //number += 100;// error 可以定义为__block 类型,就可以修改 number2 += 2000; }; number = 300; myblock2(200, 300); // 如果block 引用局部对象、实例对象时候,该对象的实例、对象会被retain, 如果加__block修饰,则不会。 NSLog(@"%d", self.retainCount); block myblock3 = ^(int a, int b){ self.view.backgroundColor = [UIColor redColor]; NSLog(@"%d", self.retainCount); }; myblock3(3, 30); }- (void)didReceiveMemoryWarning{ [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}- (void)testBlock:(block)myBlock{ myBlock(10, 30);}- (void)loadView{ [super loadView]; // block 的应用 //给button添加一个block事件 //block 循环应用的问题:在block里面引用一个实例变量,会被retain。 //解决方式就是:在使用block的地方,release block //解决方案2:使用__block修饰 //方案1 BlockButton *button = [[BlockButton alloc]initWithFrame:CGRectMake(20, 20, 100, 30)]; [button setTitle:@"button" forState:UIControlStateNormal];// __block DetailViewController *detail = [[DetailViewController alloc]init]; button.touchblock = ^(BlockButton *btn){ //将当前对象retain DetailViewController *detail = [[DetailViewController alloc]init]; [self presentViewController:detail animated:YES completion:nil]; [detail release]; NSLog(@"on click"); }; [self.view addSubview:button]; [button release]; }@end
#import <UIKit/UIKit.h>@class BlockButton;typedef void(^TouchBlock)(BlockButton *);@interface BlockButton : UIButton@property (nonatomic, copy) TouchBlock touchblock;@end#import "BlockButton.h"@implementation BlockButton- (id)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { // Initialization code [self addTarget:self action:@selector(clickAction) forControlEvents:UIControlEventTouchUpInside]; } return self;}- (void)clickAction{ self.touchblock(self);}- (void)dealloc{// //方案1// [self.touchblock release]; [super dealloc];}@end#import "DetailViewController.h"#import "BlockButton.h"@interface DetailViewController ()@end@implementation DetailViewController- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{ self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self;}- (void)viewDidLoad{ [super viewDidLoad];// Do any additional setup after loading the view.}- (void)didReceiveMemoryWarning{ [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}- (void)loadView{ [super loadView]; //方案1// UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];// button.frame = CGRectMake(20, 20, 100, 30);// [button setTitle:@"button" forState:UIControlStateNormal];// [self.view addSubview:button];// [button addTarget:self// action:@selector(onclick) forControlEvents:UIControlEventTouchUpInside];// //方案2 BlockButton *button = [[BlockButton alloc]initWithFrame:CGRectMake(20, 20, 100, 30)]; [button setTitle:@"button" forState:UIControlStateNormal]; __block DetailViewController *detail = self; button.touchblock = ^(BlockButton *btn){ //将当前对象retain [detail dismissViewControllerAnimated:YES completion:nil]; }; [self.view addSubview:button]; [button release];}- (void)onclick{ [self dismissViewControllerAnimated:YES completion:nil];}- (void)dealloc{ NSLog(@"are you dealloc"); [super dealloc];}@end
#import <UIKit/UIKit.h>typedef void(^ButtonBlock)(NSInteger);@interface Message : UIAlertView <UIAlertViewDelegate>@property (nonatomic, copy) ButtonBlock block;- (id)initWithTitle:(NSString *)title message:(NSString *)message cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles buttonBlock:(ButtonBlock)block;@end#import "Message.h"@implementation Message- (id)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { // Initialization code } return self;}- (id)initWithTitle:(NSString *)title message:(NSString *)message cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles buttonBlock:(ButtonBlock)block{ self = [super initWithTitle:title message:message delegate:self cancelButtonTitle:cancelButtonTitle otherButtonTitles:otherButtonTitles, nil]; if (self != nil){ self.block = block; } return self;}- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ _block(buttonIndex);}@endMessage *message = [[Message alloc]initWithTitle:@"title" message:@"love" cancelButtonTitle:@"okay" otherButtonTitles:nil buttonBlock:^(NSInteger index){ NSLog(@"okay"); }]; [message show]; [message release];