block代码块的使用
来源:互联网 发布:mac 编译android源码 编辑:程序博客网 时间:2024/05/23 01:47
block代码块功能类似于 delegate代理。即也用来由 B对象代理完成 A对象的事件。
区别
1、不用定义协议(协议名称、协议方法、代理);
2、实现方法比较直观(实现代码比较紧凑,即代码块方法,紧接在对象实例化之后,避免多行空隔设置)
3、定义 block属性对象时,应用 copy类型,而定义 delegate对象时应用 assign类型
4、使用 block避免循环引用导致内存泄露(常提示:capturing self strongly in this block is likely to lead to a retain cycle),而使用 delegate时不需要关注循环引用问题
注意:block解决循环引用问题,使用对象的弱引用,如:
SYViewController __weak *weakSelf = self;self.sfVC.orderTicker = ^(){ weakSelf.tickets = @"我帮你订购了两张票"; NSLog(@"%@", weakSelf.tickets);};
说明:
1、使用场景:
1-1、由其他对象协助完成其他事情
1-2、用于传值
定义了代码块属性的视图控制器
// .h文件#import <UIKit/UIKit.h>@interface SFViewController : UIViewController- (void)orderTickerForMe;- (void)getTickerForMe;@property (nonatomic, copy) void (^orderTicker)(void);@property (nonatomic, copy) void (^getTicker)(void);@end
// .m文件#import "SFViewController.h"@interface SFViewController ()@end@implementation SFViewController- (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)orderTickerForMe{ if (self.orderTicker) { self.orderTicker(); }}- (void)getTickerForMe{ if (self.getTicker) { self.getTicker(); }}@end
使用(另一个视图控制器使用定义了代码块属性的视图控制器)
#import "SYViewController.h"#import "SFViewController.h"@interface SYViewController ()@property (nonatomic, strong) NSString *tickets;@property (nonatomic, strong) SFViewController *sfVC;@end@implementation SYViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.title = @"sf block"; UIBarButtonItem *order = [[UIBarButtonItem alloc] initWithTitle:@"order" style:UIBarButtonItemStyleDone target:self action:@selector(tickerOrder)]; UIBarButtonItem *get = [[UIBarButtonItem alloc] initWithTitle:@"get" style:UIBarButtonItemStyleDone target:self action:@selector(tickerGet)]; self.navigationItem.rightBarButtonItems = @[order, get]; self.sfVC = [[SFViewController alloc] init];// // 该方法会造成循环引用,导致内存泄露// self.sfVC.orderTicker = ^(){// self.tickets = @"我帮你订购了两张票";// NSLog(@"%@", self.tickets);// }; // 使用弱引和,避免循环引用 SYViewController __weak *weakSelf = self; self.sfVC.orderTicker = ^(){ weakSelf.tickets = @"我帮你订购了两张票"; NSLog(@"%@", weakSelf.tickets); }; self.sfVC.getTicker = ^(){ NSLog(@"我帮你取了2张票"); };}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}- (void)tickerOrder{ [self.sfVC orderTickerForMe];}- (void)tickerGet{ [self.sfVC getTickerForMe];}@end
归纳总结:block使用时的定义方法大全
1、属性变量
(1)直接定义成属性(带参数)
@property (nonatomic, copy) void (^buttonClick)(UIButton *button);
(2)直接定义属性(不带参数)
@property (nonatomic, copy) void (^buttonClick)(void);
(3)先定义宏,再定义属性(带参数)
typedef void (^ButtonClick)(UIButton *button);@property (nonatomic, copy) ButtonClick buttonClick;
(4)先定义宏,再定义属性(不带参数)
typedef void (^ButtonClick)(void);@property (nonatomic, copy) ButtonClick buttonClick;
2、方法
(1)带参数的方法
- (void)buttonClick:(void (^)(UIButton *button))click;
(2)不带参数的方法
- (void)buttonClick:(void (^)(void))click;
PS:因为使用block时,需要避免循环引用造成的内存泄露,所以在block引用中被引用的变量参数,必须是弱引用(1、block属性或方法的变量是当前对象(如视图控制器)的属性;2、被使用在block内的变量也是当前对象(如视图控制器)的属性)
定义弱引用的方法
1、格式:" __weak 当前对象名称 *weakSelf = self; ",如:
__weak UIViewController *weakSelf = self;
2、格式:" __weak typeof(self) weakSelf = self; ",如:
__weak typeof(self) weakSelf = self;
- block代码块的使用
- block代码块的使用
- IOS Block代码块的简单使用
- IOS Block代码块的简单使用
- iOS 代码块(block)的使用
- Block代码块的使用,和注意事项
- 代码块(Block)的使用
- iOS中Block代码块的使用
- 关于Block代码块的使用
- iOS 代码块(block)的使用
- Objective-C语法之代码块(block)的使用
- Objective-C语法之代码块(block)的使用
- Objective-C语法之代码块(block)的使用
- Objective-C语法之代码块(block)的使用
- Objective-C代码中,块(block)的使用
- Objective-C语法之代码块(block)的使用
- Objective-C语法之代码块(block)的使用
- Objective-C语法之代码块(block)的使用
- 软件开发文档范例
- linux grep命令 1.作用 Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来。grep全称是Global Regular Express
- 山东历史沿革 (zz)
- 剑指offer系列-T11数值的整数次方
- 中科软的低调与活力—— 一家大型方案商20年的发展与变迁
- block代码块的使用
- Objective-C之Block剖析
- sql 分组查询效率
- template classes or function
- 一句SQL,判断char列的值是否组成回文字符串
- jsonp跨域问题
- 开启MyBatis(二)创建工程
- PAT乙级练习题B1031. 查验身份证
- rdd的join使用