//在需要传值的.h文件中声明block属性
#import <UIKit/UIKit.h>
@interface FirstViewController : UIViewController
@property (nonatomic, retain)UIButton *button;
#warning block 第一步 声明block属性
//类型一定为copy
@property (nonatomic, copy)void(^myBlock)(UIColor *color);
@end
#import "FirstViewController.h"
@interface FirstViewController ()
@end
@implementation FirstViewController
#warning block 第四步 属性block内存释放
- (void)dealloc
{
Block_release(_myBlock);
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor yellowColor];
self.button = [UIButton buttonWithType:UIButtonTypeCustom];
self.button.frame = CGRectMake(20, 100, 100, 40);
self.button.backgroundColor = [UIColor greenColor];
[self.button setTitle:@"第一页" forState:UIControlStateNormal];
[self.button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.button];
}
- (void)buttonAction:(UIButton *)button
{
[self.navigationController popToRootViewControllerAnimated:YES];
#warning block 第二步 执行block
self.myBlock([UIColor blackColor]);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
//在被传值的.h页面自定义一个按钮,点击按钮完成传值
#import <UIKit/UIKit.h>
#import "FirstViewController.h"
@interface RootViewController : UIViewController
@property (nonatomic, retain)UIButton *button;
@end
#import "RootViewController.h"
@interface RootViewController ()
@end
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor redColor];
self.button = [UIButton buttonWithType:UIButtonTypeCustom];
self.button.frame = CGRectMake(20, 100, 100, 40);
self.button.backgroundColor = [UIColor greenColor];
[self.button setTitle:@"根" forState:UIControlStateNormal];
[self.button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.button];
}
- (void)buttonAction:(UIButton *)button
{
FirstViewController *first = [[FirstViewController alloc]init];
[self.navigationController pushViewController:first animated:YES];
//无参数 无返回值
//block修饰 外部变量int a;
__block int a = 0;
void(^block1)(void) = ^(void)
{
a++;
NSLog(@"block1 = %d", a);
};
//调用方法
block1();
//有参数, 无返回值
void(^block2)(int a, NSString *string) = ^(int a, NSString *string)
{
NSLog(@"string = %@", string);
};
block2(10, @"大水杯");
//无参数, 有返回值
NSString *(^block3)(void) = ^(void)
{
return @"登山包";
};
NSLog(@"block3 = %@", block3());
//有参数, 有返回值
NSString *(^block4)(int a, NSString *string) = ^(int a, NSString *string)
{
return[string stringByAppendingString:@"大水杯"];
};
NSString *str = block4(11, @"124");
NSLog(@"%@", str);
#pragma mark --- 重点1.block内存管理
//当block内部使用外部变量时,系统会将内存移到栈区 Stack: 栈区
//系统分配的内存都放在栈区
//自己开辟的内存在堆区
NSLog(@"block1 = %@", block1);
//block默认放在全局区 Global: 全局区
NSLog(@"block2 = %@", block2);
#pragma mark--- 解决循环引用的方法 将self定义为block变量
__block RootViewController *rootV = self;
#warning block 第三步 block实现
first.myBlock = ^(UIColor *color)
{
// 记住 回调回来的参数 直接用, 他是另一个页面传过来的
#pragma mark ------ 重要 如果在block内部中使用self属性,会将self引用计数加一,导致循环引用,因此不能使用self,而是将self定义为block变量
rootV.view.backgroundColor = color;
};
//当将block设置成copy类型的属性时, block会被放入堆区中,需要自己手动释放内存
//release时 block在堆区
NSLog(@"myBlock = %@", first.myBlock);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end