最直接的教你OC中Block的简单使用场景
场景一: A控制器跳转到B控制器 -- B控制器事件处理通过Block回调给A控制器
A 跳转前界面如下

点击ToB按钮到控制器B

在控制器B中点击按钮返回到A界面如下

不说废话上码!!!!
A-->控制器 .m
#import "ViewControllerA.h"
#import "ViewControllerB.h" @interface ViewControllerA () @property (nonatomic, strong) ViewControllerB *controllerB; @property (nonatomic, strong) UILabel *testLabel; @end @implementation ViewControllerA - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor]; UIButton *jumpButton = [[UIButton alloc] initWithFrame:CGRectMake(, , , )];
jumpButton.backgroundColor = [UIColor redColor];
jumpButton.layer.cornerRadius = ;
[jumpButton addTarget:self action:@selector(jumpButtonClickAction) forControlEvents:UIControlEventTouchUpInside];
[jumpButton setTitle:@"ToB" forState:UIControlStateNormal];
[self.view addSubview:jumpButton]; _testLabel = [[UILabel alloc] initWithFrame:CGRectMake(, , self.view.frame.size.width - , )];
_testLabel.center = self.view.center;
_testLabel.textAlignment = NSTextAlignmentCenter;
_testLabel.backgroundColor = [UIColor lightGrayColor];
_testLabel.textColor = [UIColor redColor];
[self.view addSubview:_testLabel];
} - (void)jumpButtonClickAction { //TODO: 声明weakSelf 在Block中使用 方式Block循环引用
__weak typeof(self) weakSelf = self;
_controllerB = [[ViewControllerB alloc] init]; //TODO: 控制器中点击测试按钮 通过Block的回调实现
_controllerB.change_controllerA_labelTitleBlock = ^(NSString *title) {
weakSelf.testLabel.text = [NSString stringWithFormat:@"点击了%@",title];
}; [self.navigationController pushViewController:_controllerB animated:YES];
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end
B--> .h代码
#import <UIKit/UIKit.h> @interface ViewControllerB : UIViewController //TODO: 声明用来回调的 Block
@property (nonatomic, copy) void(^change_controllerA_labelTitleBlock)(NSString *title); @end
B--> .m代码
#import "ViewControllerB.h"
@interface ViewControllerB ()
@end
@implementation ViewControllerB
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor];
UIButton *buttonLeft = [[UIButton alloc] initWithFrame:CGRectMake(, , , )];
buttonLeft.backgroundColor = [UIColor blackColor];
[buttonLeft setTitle:@"buttonLeft" forState:UIControlStateNormal];
[buttonLeft addTarget:self action:@selector(buttonClickAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:buttonLeft];
UIButton *buttonRight = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMaxX(buttonLeft.frame) + , , , )];
buttonRight.backgroundColor = [UIColor blackColor];
[buttonRight setTitle:@"buttonRight" forState:UIControlStateNormal];
[buttonRight addTarget:self action:@selector(buttonClickAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:buttonRight];
}
- (void)buttonClickAction:(UIButton *)sender {
//TODO: 判断 self.change_controllerA_labelTitleBlock 是否为空(必写)
if (self.change_controllerA_labelTitleBlock) {
//TODO: Block 会调给控制器A 值
self.change_controllerA_labelTitleBlock(sender.titleLabel.text);
}
[self.navigationController popViewControllerAnimated:YES];
}
//TODO: Block Set方法(必写)
- (void)setChange_controllerA_labelTitleBlock:(void (^)(NSString *))change_controllerA_labelTitleBlock {
_change_controllerA_labelTitleBlock = change_controllerA_labelTitleBlock;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
场景二:A控制器中添加子View 子View中的事件回调
截图如下

A --> .m
#import "ViewControllerA.h"
#import "TestBlockView.h" @interface ViewControllerA () @property (nonatomic, strong) TestBlockView *testBlockView; @property (nonatomic, strong) UILabel *testLabel; @end @implementation ViewControllerA - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor]; _testLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 70, self.view.frame.size.width - 40, 40)];
_testLabel.textAlignment = NSTextAlignmentCenter;
_testLabel.backgroundColor = [UIColor lightGrayColor];
_testLabel.textColor = [UIColor redColor];
[self.view addSubview:_testLabel]; __weak typeof(self) weakSelf = self;
_testBlockView = [[TestBlockView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width - 100, self.view.frame.size.width - 100) testBlock:^(NSString *title) {
weakSelf.testLabel.text = [NSString stringWithFormat:@"点击了%@",title];
}]; _testBlockView.center = self.view.center;
_testBlockView.backgroundColor = [UIColor yellowColor];
[self.view addSubview:_testBlockView];
} @end
TestBlockView --> .h
#import <UIKit/UIKit.h> //TODO: 声明Block的第二种写法 typedef void(^TestBlock)(NSString *title); @interface TestBlockView : UIView @property (nonatomic, copy) TestBlock testBlock1; //TODO: 重写init方法 加上Block回调处理
- (instancetype)initWithFrame:(CGRect)frame testBlock:(TestBlock)testBlock1;
@end
TestBlockView --> .m
#import "TestBlockView.h" @implementation TestBlockView //TODO: 重写init方法 加上Block回调处理
- (instancetype)initWithFrame:(CGRect)frame testBlock:(TestBlock)testBlock1 {
if (self = [super initWithFrame:frame]) {
_testBlock1 = testBlock1;
[self addTestButton];
}
return self;
} - (void)addTestButton {
UIButton *testButton1 = [[UIButton alloc] initWithFrame:CGRectMake(, , , )];
testButton1.layer.cornerRadius = ;
testButton1.backgroundColor = [UIColor lightGrayColor];
[testButton1 setTitle:@"testButton1" forState:UIControlStateNormal];
[testButton1 addTarget:self action:@selector(testButtonClick:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:testButton1]; UIButton *testButton2 = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMaxX(testButton1.frame) + , CGRectGetMinY(testButton1.frame), , )];
testButton2.layer.cornerRadius = ;
testButton2.backgroundColor = [UIColor lightGrayColor];
[testButton2 setTitle:@"testButton2" forState:UIControlStateNormal];
[testButton2 addTarget:self action:@selector(testButtonClick:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:testButton2];
} - (void)testButtonClick:(UIButton *)sender {
if (self.testBlock1) {
self.testBlock1(sender.titleLabel.text);
}
} /*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/ @end
以上是我简单直接的介绍了项目中常用的两种Block的使用,自我感觉比起通知代理要方便的多,有疑问的小伙伴可以留言问我。
最直接的教你OC中Block的简单使用场景的更多相关文章
- OC中 block 的用法
block 常用于反向传值 声明 返回值类型 (^block)(参数列表) 调用 闭包的名字=^(参数列表){}: 闭包的名字(): 如: void(^aaaaa)(int num,NSString ...
- OC中block作方法参数时的用法
方式一.在传参时直接声明block回调方法. 1. 定义方法: - (int)doTest:(NSString *)name para1:(int)temp1 para2:(int)temp2 suc ...
- OC中RAC编程block的基本使用
在OC中block的基本使用 // // ViewController.h // RAC--test // // Created by Aaron on 17/1/17. // Copyright © ...
- swift中block的使用
在OC中习惯用block来传值,而swift中,block被重新定义了一下,叫闭包: 使用的技巧:谁定义谁传值: 案例使用A.B控制器: 1~4步在B中执行,最后在A中执行: - B控制器: 1- ...
- OC中的代理模式
OC中的代理模式,关于代理模式,如果还有同学不太清楚的话,就自己去补充知识了,这里就不做介绍了,这里只介绍OC中是如何实现代理模式的.这里举一个简单的例子:小孩类,护士类,保姆类,其中小孩类有两个方法 ...
- Swift: 比较Swift中闭包传值、OC中的Block传值
一.介绍 开发者对匿名函数应该很清楚,其实它就是一个没有名字的函数或者方法,给人直观的感觉就是只能看到参数和返回值.在iOS开发中中,它又有自己的称呼,在OC中叫Block代码块,在Swift中叫闭包 ...
- QF——OC中的SEL类型和Block
@selector(): 可以理解@selector()就是取类方法的编号,他的基本行为类似于C语言中的函数指针(指向函数的指针).它们通过传递方法的地址(或编号)来实现把方法当做参数的效果. 不过在 ...
- Objective-C中block的底层原理
先出2个考题: 1. 上面打印的是几,captureNum2 出去作用域后是否被销毁?为什么? 同样类型的题目: 问:打印的数字为多少? 有人会回答:mutArray是captureObject方法的 ...
- iOS OC语言: Block底层实现原理
先来简单介绍一下BlockBlock是什么?苹果推荐的类型,效率高,在运行中保存代码.用来封装和保存代码,有点像函数,Block可以在任何时候执行. Block和函数的相似性:(1)可以保存代码(2) ...
随机推荐
- spark单机部署及样例运行
spark单机运行部署 环境预装 需要预先下载jdk和spark.机器使用centos6.6(推荐).然后依次运行 [root@spark-master root]# cd /root #安装必要的软 ...
- 深入浅出Redis-redis底层数据结构(下)
概述: 学习使用Redis,其实并不需要去研究其底层数据的实现.我们只需要了解他有哪些常用的数据类型,然后熟练使用,就可以很好的掌握Redis 这个工具了.但是这样的学习方法只适合Redis 的入门, ...
- 利刃 MVVMLight 2:Model、View、ViewModel结构以及全局视图模型注入器的说明
上一篇我们已经介绍了如何使用NuGet把MVVMLight应用到我们的WPF项目中.这篇我们来了解下一个基本的MVVMLight框架所必须的结构和运行模式. MVVMLight安装之后,我们 ...
- ruby操作mongo DB
web自动化中,对操作日志功能进行验证(操作日志存在mongoDB中). 为了避免前面操作产生的日志影响结果校验,我们需要先清除之前的所有操作日志. require 'mongo' host = '1 ...
- Mocha的单元测试实战
Mocha Mocha是一个测试框架,为JS应用添加测试.使用见:mochajs. Mocha结合Nodejs实战 ontstair.js 这里我们使用自定义模块:ontstair.js,代码如下. ...
- Android之WebView网页滚动截图
WebView 网页滚动截屏,可对整个网页进行截屏而不是仅当前屏幕哦! 注意若Web页面存在position:fixed; 的话得在调用前设置为 position:absolute; 哦,否则会出现很 ...
- Linux之初体验
预备作业03--我的Linux初体验 学习基于VirtualBox虚拟机安装Ubuntu图文教程在自己笔记本上安装Linux操作系统 一开始以为这个项目很简单,以往也在自己的笔记本上看教程安装过软件, ...
- trove datastore 浅析
以下代码来自trove/datastore该目录下一共有4个文件__init__,views,models,service大概关系(主要是wsgi吧,没仔细学过,简单的从代码上做推测),service ...
- JS-兼容
[个人汇总] 1.JS判断是否点击回车键/enter键 $(document).keypress(function(event){ var keycode = (event.keyCode ? eve ...
- hdu1116回溯N皇后问题
题目连接 经过思考,不难发现:恰好N个皇后放在不同行不同列,那么是不是可以转换成N个皇后所在行分别确定(一人一行)的情况下对她们的所在列的枚举. 也就是列的全排列生成问题,我们用c[x]表示x行皇后的 ...