最直接的教你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) ...
随机推荐
- [ios2]OS 6 SDK: 在应用内展示App Store【转】
出于什么样的原因你会希望用户从你的iOS app中进入App Store呢?可能你想用户去App Store 为你的应用评分,也可能你希望用户看到你其他的iOS app.iOS 6引入了SKStore ...
- Java ArrayList、Vector和LinkedList等的差别与用法(转)
Java ArrayList.Vector和LinkedList等的差别与用法(转) ArrayList 和Vector是采取数组体式格式存储数据,此数组元素数大于实际存储的数据以便增长和插入元素,都 ...
- 【JS学习笔记】关于function函数
函数的基本格式 function 函数名() { 代码: } 函数的定义和调用 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transit ...
- 一名测试初学者听JAVA视频笔记(一)
搭建pho开发环境与框架图 韩顺平 第一章: No1 关于文件以及文件夹的管理 将生成的文本文档做成详细信息的形式,显示文件修改时间以及文件大小,便于文件查看和管理,也是对于一名IT人士高效能工作的 ...
- 数据持久层框架iBatis, Hibernate 与 JPA 比较
在本文中我们介绍并比较两种最流行的开源持久框架:iBATIS和Hibernate,我们还会讨论到Java Persistence API(JPA).我们介绍每种解决方案并讨论其所规定的品质,以及在广泛 ...
- 原生js
- android ViewStub延时渲染的应用
android开发当中,我们经常会遇到根据某个条件去控制一个控件的显示/隐藏的情况.虽然setVisibility(int visibility)的确可以达到这样的目的,但是在渲染时,其实隐藏的布局也 ...
- JavaScript - 平稳退化
JavaScript使用window对象的open()方法来创建新的浏览器窗口.这个方法有三个参数:window.open(url,name,features)这三个参数都是可选的.1.第一个参数是想 ...
- Vim扩展YouCompleteMe插件
在Vim中安装YouCompleteMe插件 一.安装前的说明: 1.确保vim版本>=7.4,若MAC OS,建议直接安装MacVim(8.0版本). ps:如果不想使用MacVim的GUI, ...
- table中td内容过长 省略号显示
首先设置 css样式: table { table-layout: fixed;} HTML中的table代码: <tr> <th class="col-md-1" ...