Block作为property属性实现页面之间传值(代替Delegate代理与协议结合的方法)
需求:在ViewController中,点击Button,push到下一个页面NextViewController,在NextViewController的输入框TextField中输入一串字符,返回的时候,在ViewController的Label上面显示文字内容,
(1)第一种方法:首先看看通过“协议/代理”是怎么实现两个页面之间传值的吧,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
//NextViewController是push进入的第二个页面 //NextViewController.h 文件 //定义一个协议,前一个页面ViewController要服从该协议,并且实现协议中的方法 @protocol NextViewControllerDelegate <NSObject> - ( void )passTextValue:(NSString *)tfText; @end @interface NextViewController : UIViewController @property (nonatomic, assign) id<NextViewControllerDelegate> delegate; @end //NextViewController.m 文件 //点击Button返回前一个ViewController页面 - (IBAction)popBtnClicked:(id)sender { if (self.delegate && [self.delegate respondsToSelector:@selector(passTextValue:)]) { //self.inputTF是该页面中的TextField输入框 [self.delegate passTextValue:self.inputTF.text]; } [self.navigationController popViewControllerAnimated:YES]; } |
接下来我们在看看ViewController文件中的内容,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
//ViewController.m 文件 @interface ViewController ()<NextViewControllerDelegate> @property (strong, nonatomic) IBOutlet UILabel *nextVCInfoLabel; @end //点击Button进入下一个NextViewController页面 - (IBAction)btnClicked:(id)sender { NextViewController *nextVC = [[NextViewController alloc] initWithNibName:@ "NextViewController" bundle:nil]; nextVC.delegate = self; //设置代理 [self.navigationController pushViewController:nextVC animated:YES]; } //实现协议NextViewControllerDelegate中的方法 #pragma mark - NextViewControllerDelegate method - ( void )passTextValue:(NSString *)tfText { //self.nextVCInfoLabel是显示NextViewController传递过来的字符串Label对象 self.nextVCInfoLabel.text = tfText; } |
这是通过“协议/代理”来实现的两个页面之间传值的方式。
(2)第二种方法:使用Block作为property,实现两个页面之间传值,
先看看NextViewController文件中的内容,
1
2
3
4
5
6
7
8
9
10
11
12
|
//NextViewController.h 文件 @interface NextViewController : UIViewController @property (nonatomic, copy) void (^NextViewControllerBlock)(NSString *tfText); @end //NextViewContorller.m 文件 - (IBAction)popBtnClicked:(id)sender { if (self.NextViewControllerBlock) { self.NextViewControllerBlock(self.inputTF.text); } [self.navigationController popViewControllerAnimated:YES]; } |
再来看看ViewController文件中的内容,
1
2
3
4
5
6
7
8
9
10
11
12
13
|
- (IBAction)btnClicked:(id)sender { NextViewController *nextVC = [[NextViewController alloc] initWithNibName:@ "NextViewController" bundle:nil]; nextVC.NextViewControllerBlock = ^(NSString *tfText){ [self resetLabel:tfText]; }; [self.navigationController pushViewController:nextVC animated:YES]; } #pragma mark - NextViewControllerBlock method - ( void )resetLabel:(NSString *)textStr { self.nextVCInfoLabel.text = textStr; } |
好了就这么多代码,可以使用Block来实现两个页面之间传值的目的,实际上就是取代了Delegate的功能。
Block作为property属性实现页面之间传值(代替Delegate代理与协议结合的方法)的更多相关文章
- ASP.NET 页面之间传值的几种方式
开篇概述 对于任何一个初学者来说,页面之间传值可谓是必经之路,却又是他们的难点.其实,对大部分高手来说,未必不是难点. 回想2016年面试的将近300人中,有实习生,有应届毕业生,有1-3年经验的,有 ...
- 【ASP.NET MVC系列】浅谈ASP.NET 页面之间传值的几种方式
ASP.NET MVC系列文章 [01]浅谈Google Chrome浏览器(理论篇) [02]浅谈Google Chrome浏览器(操作篇)(上) [03]浅谈Google Chrome浏览器(操作 ...
- asp.net页面之间传值方法详解
asp.net中页面之间传值我们用得最多的就是get,post这两种了,其它的如session,appliction,cookie等这些相对来说少用也不是常用的,只是在特殊情况下在使用了. 1. Ge ...
- js父页面和子页面之间传值
今天和朋友一块讨论,怎样通过js在父页面和子页面之间传值的问题,总结例如以下: 需求描写叙述:父页面有多个子页面.实如今父页面点击子页面,传值到子页面. 看着非常easy,试了好久.主要纠结在怎样获取 ...
- 【微信小程序】mpvue中页面之间传值(全网唯一真正可行的方法,中指推了一下隐形眼镜)
摘要: mpvue中页面之间传值(注意:是页面之间,不是组件之间) 场景:A页面跳转B页面,在B页面选择商品,将商品名带回A页面并显示 使用api: getCurrentPages step1: A页 ...
- 页面之间传值方式的总结,五种方式,通知,block,代理,单例,NSUERDEFALUT,
首先代码拿上 1:单例 2:通知 3:代理 4:block方法 5:NSUSERDEFAULT(沙盒文件) 先是单例类: .h文件 @interface DataSource : NSObject @ ...
- ASP.NET页面之间传值
介绍: 在网页应用程序的开发中,页面之间的传值应该是最常见的问题了. 在这篇文章里,azamsharp 将为我们介绍一些ASP.NET页面传值的方式.本文所举的例子非常简单,仅仅包含了一个文本框和几个 ...
- iOS学习之页面之间传值的方式总结
传值三种场景: 1.属性传值(从前往后传) 需求:第二个界面标签显示第一个界面输入框文字. 第一步, 在前一个界面定义属性. (语义属性声明为copy); 第二步, 在进入下一个界面之前,给属性传入数 ...
- JS页面之间传值
父页面与子页面之间有多种传值的方式: 第一种,通过window.open的方法打开一个新的页面,在新的页面里面通过window.opener来获取对象,以下为实例 父页面: function open ...
随机推荐
- BZOJ3067 : Hyperdrome
设f[i][j]表示前i个字母中字母j出现的次数对2取模的结果. 若[l,r]经过重组可以形成回文串,则需满足f[l-1][j]与f[r][j]至多有1位不同. 将f[i]用一个long long表示 ...
- topcoder 594 DIV2 foxandclassroom
暴力枚举 1 #include <iostream> #include <vector> #include <string> using namespace std ...
- HDU 4057 Rescue the Rabbit(AC自动机+DP)
题目链接 一个数组开小了一点点,一直提示wa,郁闷,这题比上个题简单一点. #include <iostream> #include <cstring> #include &l ...
- sqlserver行列转换
sqlserver行转列 --创建行转列表及插入数据 create table tb_RowConvertToColumn ( username ) null, course ) null, scor ...
- 定时任务之Spring与Quartz的整合(有修改)
转摘:http://www.javaweb1024.com/java/JavaWebzhongji/2015/04/13/548.html 在Spring中使用Quartz有两种方式实现:第一种是任务 ...
- redis_查找命令
1:文件名查找 find -name "filename" filename可以用通配符 2:文件内行数查找 在vi模式下 ,刚进入vi 输入:128(linenum) 即能跳 ...
- Flex httpservice返回值类型和处理 (转)
这两天在考虑flex与后端java服务交互的问题.在采用BlazeDS的Remote Object方式,还是传统的http service方式之间徘徊了一段时间 采用BlazeDS的Remote Ob ...
- mysql5.6安装
mysql5.6安装 #卸载原有的mysqlyum remove mysql*ls /etc/my.cnf*mv /etc/my.cnf* /tmp/ #安装依赖包yum install make c ...
- service mongod start start: Unknown job: mongod问题
终于解决了这个异常蛋疼的问题,当安装完毕mongodb的时候,执行: root@ubuntu:/usr/local# service mongod start 出现: start: Unknown j ...
- Physically Based Render in Game 序
基于物理渲的渲染理论,从SIGGRAPH06被Naty Hoffman等人提出后,近年来也越来越多的被各大游戏公司所采用,几乎已经是次世代游戏的标准特性,也是每个3D游戏工作者所必备的知识,尽管每年S ...