ios 得用代理反向传值
应用场景:有时时候从界面A跳转到界面B,界面B在返回的时候须要将处理的结果传递给A.
实现思路:1,定义一个负责传值的协义,界面A拥有该协义属性,并实现该协义中的方法
2。界面B也拥有该协义属性(代理要求两者都具有同样对象的引用 ),然后在返回的时候获取界面A的引用指针,而且指定B中协义的调用目标为A,调用协义中的传值方法.
详细代码:
A的头文件 :
#import <UIKit/UIKit.h>
@protocol passValueDelegate <NSObject>
-(void) setValue:(NSString *)param;
@end
@interface ViewController :
UIViewController
@property
id<passValueDelegate> passValueDelegate;
@end
#import "ViewController.h"
#import "ViewController2.h"
@interface
ViewController ()
{
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super
viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIButton *btn =[UIButton
buttonWithType:UIButtonTypeRoundedRect];
btn.,
100, 200,
40);
[btn setTitle:@"btn"
forState:UIControlStateNormal];
[btn addTarget:self
action:@selector(btn)
forControlEvents:UIControlEventTouchUpInside];
[self.view
addSubview:btn];
self.view.backgroundColor=[UIColor
redColor];
}
-(void) setValue:(NSString *)param{
NSLog(@"pass value is:%@",param);
}
-(void)btn
{
[self.navigationController pushViewController:[[ViewController2 alloc]init] animated:YES];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
#import <UIKit/UIKit.h>
@interface ViewController2 :
UIViewController
@end
#import "ViewController2.h"
#import "ViewController.h"
@interface
ViewController2 ()
@property
id<passValueDelegate> passValueDelegate;
@end
@implementation ViewController2
- (void)viewDidLoad
{
[super
viewDidLoad];
UIButton *btn =[UIButton
buttonWithType:UIButtonTypeRoundedRect];
btn.,
100, 200,
40);
[btn setTitle:@"back"
forState:UIControlStateNormal];
[btn addTarget:self
action:@selector(btn)
forControlEvents:UIControlEventTouchUpInside];
[self.view
addSubview:btn];
}
-(void)btn
{
// NSLog(@"%@",self.navigationController.childViewControllers[0]);
];
[self.navigationController
popToRootViewControllerAnimated:YES];
[self.passValueDelegate
setValue:@"123456789"];
}
- (void)didReceiveMemoryWarning
{
[super
didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
ios 得用代理反向传值的更多相关文章
- OC10_代理反向传值
// // ProtectedDelegate.h // OC10_代理反向传值 // // Created by zhangxueming on 15/6/24. // Copyright (c) ...
- iOS 代理反向传值
在上篇博客 iOS代理协议 中,侧重解析了委托代理协议的概念等,本文将侧重于它们在开发中的应用. 假如我们有一个需求如下:界面A上面有一个button.一个label.从界面A跳转到界面B,在界面B的 ...
- iOS-设计模式之代理反向传值
代理设计模式就是自己的方法自己不实现,让代理对象去实现. 可以让多个类实现一组方法. 委托模式的好处在于: 1.避免子类化带来的过多的子类以及子类与父类的耦合 2.通过委托传递消息机制实现分层解耦 代 ...
- 利用协议代理实现导航控制器UINavigationController视图之间的正向传值和反向传值
实验说明 (1)正向传值:比如A类里地值要传给B类用,就是我们先在A类中声明一个B类对象(当然B类头文件要import过来),然后把A类中得某个 值传递给B类中得某个值(所以需要在B类中先准备一个变量 ...
- iOS Block界面反向传值
在上篇博客 <iOS Block简介> 中,侧重解析了 iOS Block的概念等,本文将侧重于它们在开发中的应用. Block是iOS4.0+ 和Mac OS X 10.6+ 引进的对C ...
- IOS Block 反向传值
1.在需要像上一个界面传值的.h 文件实现代理方法 @property (nonatomic, copy) void(^isOpenHandler)(BOOL) ; 2.在执行操作的时候需要江操作的结 ...
- 03-UIKit、VC之间正向反向传值、代理
目录: 一.正向传值 二.反向传值 三.代理模式 回到顶部 正向传值:就是把第一个界面的值传给第二个界面显示,其简单实现方法 1 首先在第一个界面中要有一个textField输入框,一个按钮butto ...
- [ios][swift]使用swift闭包进行viewcontroller反向传值
闭包参考:http://c.biancheng.net/cpp/html/2285.html 闭包详解 传值参考:http://www.tuicool.com/articles/vy2uUz Sw ...
- IOS 学习笔记 2015-04-15 控制器数据反向传值
// // FirstViewController.h // 控制器数据传递 // // Created by wangtouwang on 15/4/15. // Copyright (c) 201 ...
随机推荐
- 如何下载 Nginx (windows 版本)并且简单的使用
官网地址:http://nginx.org/ 进到官网 我这里下载的是 稳定版的 windows版本. 开始我们的简单测试 步骤一:找到nginx的压缩包,(随意找个地方)解压 步骤二:进入conf文 ...
- Python随笔-函数
在Python中,定义一个函数要使用def语句,依次写出函数名.括号.括号中的参数和冒号:,然后,在缩进块中编写函数体,函数的返回值用return语句返回. def GetMax(v1, v2):#注 ...
- 笔记《精通css》第5章 链接应用样式
第5章 链接应用样式 1.链接伪类选择器 a : link{ } (寻找没有被访问过的链接) a : visied{ }(寻找被访问过的链接) 动态伪类选择器 a : hover ...
- Ch03 React/JSX/Component 簡介
Facebook 本身有提供 Test Utilities,但由于不够好用,所以目前主流开发社群比较倾向使用 Airbnb 团队开发的 enzyme,其可以与市面上常见的测试工具(Mocha.Karm ...
- Codeforces_733C
C. Epidemic in Monstropolis time limit per test 1 second memory limit per test 256 megabytes input s ...
- mybatis 项目配置
第一:environments MyBatis 支持多个环境,可以任意配置: 第二:transactionManager MyBatis 支持两种类型的事务管理器:JDBC 和 MANAGED(托管) ...
- Mapreduce代码疑点(1)
一.Hadoop MultipleInputs.addInputPath 读取多个路径 https://blog.csdn.net/t1dmzks/article/details/76473905 M ...
- JAVA经典题--计算一个字符串中每个字符出现的次数
需求: 计算一个字符串中每个字符出现的次数 思路: 通过toCharArray()拿到一个字符数组--> 遍历数组,将数组元素作为key,数值1作为value存入map容器--> 如果k ...
- MongoDB - 增删改查及聚合操作
目录 MongoDB - 增删改查及聚合操作 一. 数据库操作(database) 1. 创建及查看库 2. 删除库 二. 集合collectionc=操作(相当于SQL数据库中的表table) 1. ...
- 《hello-world》第八次团队作业:Alpha冲刺-Scrum Meeting 1
项目 内容 这个作业属于哪个课程 2016级计算机科学与工程学院软件工程(西北师范大学) 这个作业的要求在哪里 实验十二 团队作业8:软件测试与Alpha冲刺 团队名称 <hello--worl ...