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 ...
随机推荐
- iOS动画——CoreAnimation
CoreAnimation在我之前的UIKit动画里面简单的提了一句CoreAnimation动画,其实大家别看它类库名种有个animation,实际上animation在这个库中只占有很小的地位. ...
- vue杂记
VUE杂记 声明式渲染 <div id="app"> {{ message }} </div> var app = new Vue({ el: '#app' ...
- python gdal 矢量转栅格
data = gdal.Open(templateTifFileName, gdalconst.GA_ReadOnly)geo_transform = data.GetGeoTransform()x_ ...
- C#通过SqlConnection连接查询更新等操作Sqlserver数据库
Sqlserver数据库连接方式有多种,这里只介绍最常用的通过SqlConnection和Sqlserver数据库用户名和密码验证来进行操作数据库. 数据库连接字符串: string connStri ...
- Core 项目下使用SQl语句
public static DataTable ExeQuery(string connectionString, CommandType cmdType, string cmdText) { usi ...
- Memcached 之PHP实现服务器集群一致性hash算法
/** * memcached 一致性hash,分布式算法 * Class MemcacheCluster */ class MemcacheCluster { protected $nodes = ...
- kernel-内核抢占
kernel-内核抢占 这里有两个概念,内核抢占与用户态抢占.什么是内核抢占?就是指程序执行系统调用的时候(也就是执行于内核态的时候)被其他内核线程抢占走了. 有2种情况是不会也不应该被抢占的: 内核 ...
- Coefficient Computation (大整数、Java解决)
Coefficient Computation UVALive8265 题意:计算组合数C(n,k)的值并将值按给定的进制输出. 思路:Java大整数类硬上. PS:刚刚学完Java的大整数类,结果却 ...
- JAVA正则表达式matcher.find()和 matcher.matches()的区别
1.find()方法是部分匹配,是查找输入串中与模式匹配的子串,如果该匹配的串有组还可以使用group()函数.matches()是全部匹配,是将整个输入串与模式匹配,如果要验证一个输入的数据是否为数 ...
- Python - 三大器 迭代器,生层器,装饰器
目录 Python - 三大器 迭代器,生层器,装饰器 一. 容器 二. 可迭代对象(iterable) 三. 迭代器 四. 生成器 五. 装饰器 1. 定义 六. 闭包 Python - 三大器 迭 ...