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 ...
随机推荐
- javascript,HTML,PHP,ASP做301跳转代码 SEO优化设置
URL HTTP Redirection URL http redirection is an automatic URL change operation from one URL to anoth ...
- HDU 4604 Deque(最长上升子序)
题目链接 本来就对N*log(N)算法不大会....然后各种跪了,求出最长不下降+最长不上升-最少相同元素.求相同元素,用二分求上界搞的.代码里4个二分.... #include <cstdio ...
- maven工程代码关联源代码配置
最近在学习dubbo,在maven构建完成后,需要关联查看一些依赖jar的源码,配置很简单,如下: 勾选windows-Preferences-Maven- Download Artifact Sou ...
- 【iHMI43 4.3寸液晶模块】demo例程(库函数版)发布
一.说 明: 1.iHMI43 演示程序(0.15版)的库函数版:此程序仅用于 iHMI43 液晶模块: 2.各外设驱动均使用库函数模式操作,其他程序可参考本例子修改: 3.如果下载不进去,请重新配置 ...
- [ZZ] 在windows上编译Mesa3d opengl32库
在windows上编译Mesa3d opengl32库 cheungmine http://blog.csdn.net/ubuntu64fan/article/details/8061475 Mesa ...
- 【IOS笔记】Views
Views Because view objects are the main way your application interacts with the user, they have many ...
- memcached使用详解
不错的文章 http://www.ttlsa.com/memcache/memcached-description/
- communication between threads 线程间通信 Programming Concurrent Activities 程序设计中的并发活动 Ada task 任务 Java thread 线程
Computer Science An Overview _J. Glenn Brookshear _11th Edition activation 激活 parallel processing 并行 ...
- Android Studio 快捷键使用
最近开始全面转向Android Studio开发了,经常要去查快捷键,索性汇总下,自己方便查找 IDE 按键 说明 F1 帮助 Alt(Option)+F1 查找文件所在目录位置 Alt(Opt ...
- 设置session失效时间
以X5部署在Tomcat上为例,说明如何设置session失效时间. 可以设置session失效时间的地点有三处,分别是 1.BusinessServer的session设置 \runtime\Bus ...