// block 的简单使用demo 首先创建两个ViewController
#import "RootViewController.h"
#import "SencondViewController.h"
typedef void(^myBlock)(int ,UIButton*);
@interface RootViewController ()
{
UILabel *label;
myBlock block;
}
@end
@implementation RootViewController
- (void)viewDidLoad
{
[super viewDidLoad];
label = [[UILabel alloc]initWithFrame:CGRectMake(self.view.frame.size.width/2-75, 100, 150, 50)];
label.backgroundColor = [UIColor brownColor];
label.font = [UIFont boldSystemFontOfSize:30];
label.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:label];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.bounds = CGRectMake(0, 0, 100, 100);
btn.center = CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height/2);
[btn setTitle:@"present" forState:UIControlStateNormal];
[btn.titleLabel setFont:[UIFont systemFontOfSize:25]];
[btn setBackgroundColor:[UIColor greenColor]];
[btn addTarget:self action:@selector(presentSencondVC) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
- (void)presentSencondVC
{
SencondViewController *sencondVC = [[SencondViewController alloc]init];
block = ^(int a,UIButton*btn)
{
NSLog(@"%@",btn);
self.view.backgroundColor =[UIColor redColor];
label.text = [NSString stringWithFormat:@"%d",a];
};
sencondVC.backBlock = block;
[self presentViewController:sencondVC animated:YES completion:nil];
[sencondVC release];
}
#import <UIKit/UIKit.h>
typedef void(^callBack)(int,UIButton*);
@interface SencondViewController : UIViewController
@property(nonatomic,copy)callBack backBlock;
@end
#import "SencondViewController.h"
typedef void(^myblock)(void);
@interface SencondViewController ()
{
myblock testBlock;
}
@end
@implementation SencondViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor redColor];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.bounds = CGRectMake(0, 0, 100, 100);
btn.center = CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height/2);
[btn setTitle:@"dismiss" forState:UIControlStateNormal];
[btn setBackgroundColor:[UIColor grayColor]];
[btn addTarget:self action:@selector(dismissTopVC:) forControlEvents:UIControlEventTouchUpInside];
btn.tag = 100;
[self.view addSubview:btn];
//arc模式下,不能使用self 或者全局变量,属性
// __weak __block SencondViewController *sencondVC = self;
testBlock = [^{
[self printf];
}copy];
}
- (void)dismissTopVC:(UIButton*)button
{
//调用block,这时rootVC的block马上机会执行
self.backBlock(button.tag,button);
testBlock();
//释放block
Block_release(testBlock);
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)printf
{
NSLog(@"123");
}
- (void)dealloc
{
NSLog(@"%s",__FUNCTION__);
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}