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 ...
随机推荐
- BZOJ3757 : 苹果树
树上莫队,有几个坑: a可能等于b 数据范围是100000 #include<cstdio> #include<algorithm> #include<cmath> ...
- Java多线程编程详解
转自:http://programming.iteye.com/blog/158568 线程的同步 由于同一进程的多个线程共享同一片存储空间,在带来方便的同时,也带来了访问冲突这个严重的问题.Ja ...
- JAVA计算文件大小
File f = new File(save_path+File.separator + resouce_id+".zip"); FileInputStream fis = new ...
- 【BZOJ】1082: [SCOI2005]栅栏(二分+dfs)
http://www.lydsy.com/JudgeOnline/problem.php?id=1082 题意:n个给出木板,m个给出木板.可以将那m个木板锯成泥想要的长度.问最大能锯成多少个给出的n ...
- windows环境下nginx的启动、停止
Windows下Nginx的启动.停止等命令 在Windows下使用Nginx,我们需要掌握一些基本的操作命令,比如:启动.停止Nginx服务,重新载入Nginx等,下面我就进行一些简单的介绍.1.启 ...
- 基于开源Dubbo分布式RPC服务框架的部署整合
一.前言 Dubbo 作为SOA服务化治理方案的核心框架,用于提高业务逻辑的复用.整合.集中管理,具有极高的可靠性(HA)和伸缩性,被应用于阿里巴巴各成员站点,同时在包括JD.当当在内的众多互联网项目 ...
- Programming with gtkmm 3
https://developer.gnome.org/gtkmm-tutorial/unstable/index.html.zh_CN 1. 序言 1.1. 本书 1.2. gtkmm 2. 安 ...
- CUTE FTP 控制连接已关闭
使用Cute FTP连接FTP站点时,出现上述错误,在另外一台电脑上却可以正常连接. 原因:FTP服务器IP访问规则的限制 解决方法:在ServerU 服务器中进入服务器详细信息配置界面,在IP访问规 ...
- C# - Try catch 中 使用 End()
如果在Try中执行End()会被扑捉到.这通常并不是我想要捕捉的错误.而是结束页面的手段.可通过调整为以下代码结构修复 //this.DbHelp.BeginTransaction(); //设置回滚 ...
- fastjson解析任意json
fastjson解析任意json到bean 解析案例的代码 package com.base.config; import java.util.List; import com.alibaba.fas ...