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 ...
随机推荐
- 4.1 avd
6.接着我们回到文件夹界面,运行 AVD Manager.exe. 7.打开 AVD Manager.exe后,点击“New"创建新的模拟器: 8.创建一个新的 Android Virtua ...
- Remove Duplicates from Sorted List II leetcode java
题目: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct ...
- telerik 某些ajax拿数据方式下 load on demand 不起作用
方法一:加入以下代码 <%= Html.Telerik().ScriptRegistrar().DefaultGroup(group => { group.Add("teleri ...
- ext4 文件系统的一些记录
https://www.kernel.org/doc/Documentation/filesystems/ext4.txt ext4 权威说明 http://computer-forensics.sa ...
- python requests库学习
Python 第三方 http 库-Requests 学习 安装 Requests 1.通过pip安装 $ pip install requests 2.或者,下载代码后安装: $ git clone ...
- php 即使客户端或者服务器断开(如关掉浏览器)脚本也可以继续执行
<?php ignore_user_abort(); // run script in background set_time_limit(0); // run script forever $ ...
- 提升 web 应用程序的性能(一)
提升 web 应用程序的性能,找出瓶颈,加快客户端内容的速度. 作为 web 用户,我们知道页面加载或刷新的速度对其成功至关重要.本文将帮助您更好地理解影响 web 应用程序性能的因素.学 ...
- 你们以为运营商只是HTTP插点广告而已么?
国内某邮件服务商,近期在某南方地区有大量客户反应登录时出错和异常,于是工作人员进行了一下跟进,发现如下: 首先,邮件服务商登陆页面为普通HTTP协议发送,提交时通过JS进行RSA加密(没错,JS的RS ...
- 懒加载 字典转模型 自定义cell
1 懒加载: 1> 什么是懒加载? 懒加载又称为延时加载,即在系统调用的时候加载,如果系统不调用则不会加载.所谓的懒加载其实就是重写其 get 方法. 2> 特点:在使用懒加载的时候要 ...
- 手动编译安装lanmp centos6.5 64位
对于新手来说一个很大的问题就是连源码包都在到在哪下载,还有就是软件的依赖关系 如果网卡也不会配置,请翻看我的其他文章 这就是基本所需的源码包了 http://pan.baidu.com/s/1kTxb ...