Block高级用法(传值)

时间:2022-02-28 23:16:19

Block传值比较适用于从后一页向前一页传值

先回顾一下block的基本用法:

1. block的一般写法

定义block

void (^block)(int a) = ^(int a) {
NSLog(@"block执行: %d", a);
};
block的使用(执行bloc中的代码)

block(10);<span style="white-space:pre"></span>// block中a = 10;

2. 含有block类型的参数的方法调用

// 添加一个使用block的方法

- (void)testBlock:(void(^)(int a) )block
{
block(30);
}

// 使用方法

[self testBlock:block];

3. block类型重定义

// 把void(^)(int a)类型 重定义为BLOCK

typedef void(^BLOCK)(UIColor);

// 使用新定义的BLOCK变量定义

BLOCK b = ^(int a) {
NSLog(@"----");
};
b(10);

下面开始进行block传值

准备工作: 创建两个视图控制器(本文为MainViewController, SecondView), 添加一个导航控制器

#import "AppDelegate.h"
#import "MainViewController.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];

MainViewController *mainVC = [[MainViewController alloc] init];
UINavigationController *naviVC = [[UINavigationController alloc] initWithRootViewController:mainVC];
[[self window] setRootViewController:naviVC];

[mainVC release];
[naviVC release];
[_window release];
return YES;
}

- (void)dealloc
{
[_window release];
[super dealloc];
}
1. 在第二页上定义一个block属性 一定要用copy特性

#import <UIKit/UIKit.h>

// 重定义一个Block类型
typedef void(^BL)(UIColor *color);

@interface SecondViewController : UIViewController

// 1. 定义一个block属性 一定要使用copy特性
@property (nonatomic, copy) BL block;

@end

2. 在button触发方法中调用block

注意block的释放方法

#import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

- (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.

UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(40, 140, 240, 100);
[button setTitle:@"" forState:UIControlStateNormal];
[button setBackgroundColor:[UIColor orangeColor]];
[button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];

}

- (void)dealloc
{
Block_release(_block);
[super dealloc];
}

- (void)buttonClicked:(UIButton *)button
{
// 2. 调用
self.block([UIColor redColor]);

[self.navigationController popViewControllerAnimated:YES];
}


#import "MainViewController.h"
#import "SecondViewController.h"

// 把void(^)(int a)类型 重定义为BLOCK
typedef void(^BLOCK)(UIColor *color);

@interface MainViewController ()

@end

@implementation MainViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}

- (void)testBlock:(void(^)(int a) )block
{
NSLog(@"啦啦啦瓦洛兰");
}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.

UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
[button setFrame:CGRectMake(40, 140, 240, 100)];
[button setTitle:@"推出第二页面" forState:UIControlStateNormal];
[button setBackgroundColor:[UIColor orangeColor]];
[button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[[self view] addSubview:button];

void (^block)(int a) = ^(int a) {
NSLog(@"block 执行: %d", a);
};
block(10);
void (^block1)(int a) = ^(int a) {
NSLog(@"aa");
};
block1 = block;
block1(15);

[self testBlock:block];

BLOCK b = ^(UIColor *color) {
NSLog(@"ad");
};
b([UIColor redColor]);

}

- (void)buttonClicked:(UIButton *)button
{
BLOCK b = ^(UIColor *color) {
[[self view] setBackgroundColor:color];
};
SecondViewController *secondVC = [[SecondViewController alloc] init];
[secondVC setBlock:b];

[[self navigationController] pushViewController:secondVC animated:YES];
[secondVC release];
}