场景一: 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的简单使用场景的更多相关文章

  1. OC中 block 的用法

    block 常用于反向传值 声明 返回值类型 (^block)(参数列表) 调用 闭包的名字=^(参数列表){}: 闭包的名字(): 如: void(^aaaaa)(int num,NSString ...

  2. OC中block作方法参数时的用法

    方式一.在传参时直接声明block回调方法. 1. 定义方法: - (int)doTest:(NSString *)name para1:(int)temp1 para2:(int)temp2 suc ...

  3. OC中RAC编程block的基本使用

    在OC中block的基本使用 // // ViewController.h // RAC--test // // Created by Aaron on 17/1/17. // Copyright © ...

  4. swift中block的使用

    在OC中习惯用block来传值,而swift中,block被重新定义了一下,叫闭包: 使用的技巧:谁定义谁传值:   案例使用A.B控制器: 1~4步在B中执行,最后在A中执行: - B控制器: 1- ...

  5. OC中的代理模式

    OC中的代理模式,关于代理模式,如果还有同学不太清楚的话,就自己去补充知识了,这里就不做介绍了,这里只介绍OC中是如何实现代理模式的.这里举一个简单的例子:小孩类,护士类,保姆类,其中小孩类有两个方法 ...

  6. Swift: 比较Swift中闭包传值、OC中的Block传值

    一.介绍 开发者对匿名函数应该很清楚,其实它就是一个没有名字的函数或者方法,给人直观的感觉就是只能看到参数和返回值.在iOS开发中中,它又有自己的称呼,在OC中叫Block代码块,在Swift中叫闭包 ...

  7. QF——OC中的SEL类型和Block

    @selector(): 可以理解@selector()就是取类方法的编号,他的基本行为类似于C语言中的函数指针(指向函数的指针).它们通过传递方法的地址(或编号)来实现把方法当做参数的效果. 不过在 ...

  8. Objective-C中block的底层原理

    先出2个考题: 1. 上面打印的是几,captureNum2 出去作用域后是否被销毁?为什么? 同样类型的题目: 问:打印的数字为多少? 有人会回答:mutArray是captureObject方法的 ...

  9. iOS OC语言: Block底层实现原理

    先来简单介绍一下BlockBlock是什么?苹果推荐的类型,效率高,在运行中保存代码.用来封装和保存代码,有点像函数,Block可以在任何时候执行. Block和函数的相似性:(1)可以保存代码(2) ...

随机推荐

  1. 关于ocx中遇到的坑

    前言 这还是第一次写博客,以前太懒了,现在发现是很有必要记录下这些经验和问题的.最近项目中有个需求(报表单据需要客户签名,连接签字板,把签名单据同步到服务器上),需要和硬件交互,当时硬件商提供了ocx ...

  2. 将前台input中的数据异步传到后台并存入数据库

    将前台input中的数据异步传到后台并存入数据库 先看图: 利用ajax异步交互数据,并不是以json数组的形式将数据传到后台,而是利用字符数组的形式将其传到后台.动态新增每一行数据,将每一列对应的数 ...

  3. bzoj1298题解

    [题意分析] 要求设计一组n个m面的骰子,使每一个骰子i对骰子a[i]的胜率都大于50%. [算法分析] 对于每个i,连一条从i指向a[i]的边,那么题目给出的关系构成了一个有向基环树森林. 对于树上 ...

  4. 各种Python小玩意收集

    快速排序 quicksort = lambda lst: [] if not lst else quicksort([i for i in lst[1:] if i <= lst[0]]) + ...

  5. 想入门webpack,这篇就够了

    申明:本文转载自简书 文/zhangwang(简书作者)原文链接:http://www.jianshu.com/p/42e11515c10f#著作权归作者所有,转载请联系作者获得授权,并标注" ...

  6. javascript实现页面右侧在线客服始终跟随鼠标滚动而上下滚动且始终位于屏幕中间

    效果如图,右侧的联系一栏始终位于页面的中间位置,且随着页面的上下滚动而滚动跟随 css的话没什么好说的,看图 代码 window.onload=window.onresize=window.onscr ...

  7. 【angular】angular实现简单的tab切换

    html: <div class="list-group" ng-repeat="tab in menuList"> <a href=&quo ...

  8. HttpURLConnection传JSON数据

    try { //创建连接 URL url = new URL(url); HttpURLConnection connection = (HttpURLConnection) url.openConn ...

  9. Cracking the Coding Interview 第二章

    2.2 链表中倒数第k个结点 输入一个链表,输出该链表中倒数第k个结点. 思路:快慢指针(error: 判断是否有可行解,否则返回null, while, if 后加空格) /* public cla ...

  10. DUIlib使用Fastreport--自定义的数据

    报表根据数据源的可以分为拉模式和推模式,拉模式就是在报表中添加数据源组件从数据库中拉取数据,我们上篇报表的简单使用就是拉模式.而推模式就是在程序中构造数据托给报表显示.这篇我们这要说的是推模式. 在程 ...