iOS-Block的多种使用

时间:2022-03-09 04:01:08

一.图

iOS-Block的多种使用

二.代码

1.viewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@end

2. viewController.m

#import "ViewController.h"

#import "SecondViewController.h"

@interface ViewController ()

@end

/** 什么是Block

1. 苹果推荐的类型,效率高,在运行中保存代码.用来封装和保存代码,有点像函数,Block可以在任何时候执行.

2. Block和函数的相似性:  (1) 可以保存代码

(2) 有返回值

(3) 有形参

(4) 调用方式一样

3. 标识符 ^

*/

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

// block 1   ---> 定义一个Block类型答应一个for循环

[self block1];

// block 2   ---> 定义一个block类型的变量,接受两个参数,做加法运算

[self block2];

// block 3   ---> bolck的基本使用

[self block3];

//block 4    ---> Block访问外部变量

[self block4];

//block 5    ---> 利用tpedef定义block类型

[self block5];

//block 6   ----> 页面传值

[self block6];

}

#pragma mark 定义一个Block类型答应一个for循环

- (void)block1

{

//定义一个Block类型:名字叫MyBlock

void(^FirstBlock)(int) = ^(int n)

{

NSLog(@"%d",n);

for (int i = 0; i < n; i ++)

{

NSLog(@"n: %d",i);

}

};

//调用block

FirstBlock(5);

NSLog(@"--结束--");

/** Block执行的顺序

1. 先执行定义的Block--> FirstBlock.

2. 调用Block,获取到参数值,然后执行Block语句块.

*/

}

#pragma mark 两个参数的Block

- (void)block2

{

/** Block

1. 带有返回值的Block

2. 两个参数的Block

*/

int (^SecondBlock)(int,int) = ^(int n1, int n2)

{

return n1 + n2;

};

// 调用block

NSLog(@"%d",SecondBlock(12,14));

}

#pragma mark block的基本使用

- (void)block3

{

/** block基本使用

1. 定义Block

(1) 无参数,返回值类型为空

void (^ Block)()

(2) 有参数,返回值为 int

int (^ Block)(int)

2. 利用block封装代码块

*/

// 利用block封装代码块

// (1) 无参数无返回值

void (^FirstBlcok)() = ^()

{

NSLog(@"10");

};

//调用Block

FirstBlcok();

// (2) 有参数无返回值

NSString * (^SecondBlock)() = ^()

{

NSLog(@"无参数的有返回值的Blcok");

return @"无参数的有返回值的Blcok";

};

//调用Block

SecondBlock();

// (3) 有返回值有参数类型

int (^ThirdBlock)(int,int) = ^(int n1,int n2)

{

return n1 + n2;

};

//调用Block --> ThirdBlock(1,2)返回值类型是int

NSLog(@"%d",ThirdBlock(1,2));

}

#pragma mark block访问外部变量

- (void)block4

{

/**

1. block内部可以访问外部变量;

2. 默认情况下,Block内部不能修改外部的局部变量.

3. 给局部变量加上__block关键字,则这个局部变量可以在block内部进行修改.

*/

__block int a = 0;

void (^fourthBlcok)(int) = ^(int n1)

{

a = n1;

NSLog(@"a: %d",a);

};

fourthBlcok(10);

}

#pragma mark 利用tpedef定义block类型

- (void)block5

{

typedef int(^FifthBlock)(int,int);

FifthBlock a,b;

a = ^(int a,int b)

{

return a + b;

};

FifthBlock b2 = ^(int n1,int n2)

{

return n1 * n2;

};

NSLog(@"%@-%@-%@",a,b,b2);

}

- (void)block6

{

UIButton * button6 = [UIButton buttonWithType:UIButtonTypeCustom];

button6.frame = CGRectMake(10, 30, 200, 40);

button6.backgroundColor = [UIColor orangeColor];

[button6 addTarget:self action:@selector(block6JumpClicked) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:button6];

SecondViewController * secondViewController = [[SecondViewController alloc] init];

secondViewController.changeColorBlock = ^(UIColor * color)

{

self.view.backgroundColor = color;

};

}

- (void)block6JumpClicked

{

/** 页面传值Block注意点

1. 在传值页面   用typedef定义Block

2. 在传值页面   创建block属性

3. 在传值页面   模态跳转回来的时候,调用该Block

4. 在接收值页面 实现block代码块

*/

SecondViewController * secondViewController = [[SecondViewController alloc] init];

//实现block

secondViewController.changeColorBlock = ^(UIColor * color)

{

self.view.backgroundColor = color;

};

[self presentViewController:secondViewController animated:YES completion:^{

}];

}

@end

3. SecondViewController.h

#import <UIKit/UIKit.h>

typedef void(^ChangeColorBlock)(UIColor * color);

@interface SecondViewController : UIViewController

@property (nonatomic, strong) ChangeColorBlock changeColorBlock;

@end

4.SecondViewController.m

#import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

- (void)viewDidLoad {

[super viewDidLoad];

self.view.backgroundColor = [UIColor redColor];

[self createButton];

}

- (void)createButton

{

UIButton * button6 = [UIButton buttonWithType:UIButtonTypeCustom];

button6.frame = CGRectMake(10, 30, 200, 40);

button6.backgroundColor = [UIColor orangeColor];

[button6 addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:button6];

}

- (void)back

{

//调用block

if (self.changeColorBlock)

{

self.changeColorBlock(self.view.backgroundColor);

}

[self dismissViewControllerAnimated:YES completion:^{

}];

}

@end