最直接的教你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) ...
随机推荐
- DotNet加密方式解析--对称加密
离过年又近了一天,回家已是近在咫尺,有人欢喜有人愁,因为过几天就得经历每年一度的装逼大戏,亲戚朋友加同学的各方显摆,所以得靠一剂年终奖来装饰一个安稳的年,在这里我想起了一个题目“论装逼的技术性和重要性 ...
- POSIX多线程——基本线程管理函数介绍
POSIX基本的几个线程管理函数见下表: ------------------------------------------------------------------------------- ...
- 创建基本的2D场景(part2)
让我们继续来学习Unity2D游戏场景的制作,本文分为以下3个部分: · 添加角色和控制 . 添加2D物理阻挡 · 添加2D效果 通过制作一个移动帽子接保龄球的小游戏,我们可以学习到任何创建游戏对象, ...
- HostingEnvironment RegisterObject和QueueBackgroundWorkItem
其实网上关于HostingEnvironment 的RegisterObject和QueueBackgroundWorkItem文章已经很多了,典型是的 QueueBackgroundWorkItem ...
- url截取
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- 同盾安卓 Android应用 集成步骤:
Android SDK 新版 Android SDK 旧版 1.点击下载最新SDK(当前版本3.0.3),并解压fraudmetrix-xxx.zip文件.解压后文件目录为: fm-core-xxx ...
- 超赞网页背景效果-canvas-nest.js
canvas-nest.js 是 canvas 上绘制的蜂窝状网站背景. 引入的时候的注意事项:js加载的时候需要保证body已经加载: 一个简单的demo: <!DOCTYPE html> ...
- 利用VR技术,走进白宫,走进奥巴马
奥马巴卸任演讲由诺基亚OZO全程360° VR直播,并不是每个人都能去白宫拜访,一窥白宫内部的模样,更不可能有多少人有机会在奥马巴面前听他讲讲他的家. 现在前任总统巴拉克奥巴马以及其夫人米歇尔奥巴 ...
- [Python]-类型转换
1.字符串到数值的转换:int(s [,base ]) 将表达式s转换为一个整数 ,s可以是整数,与数字有关的字符串,布尔类型long(s [,base ]) 将表达式s转换为一个长整数 s可以是整数 ...
- [MFC美化] MFC界面UI库总结
稍微说下自己用过的感受: 1.SkinMagic 动态库DLL使用,(有VC6版本的静态链接库,没能成功调用).对控件:菜单和下拉框(下拉滚动条)有问题.不能自由设置颜色背景 皮肤格式:.smf,可使 ...