应用场景:有时时候从界面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


  A的实现文件 :

#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


B的头文件 :

#import <UIKit/UIKit.h>

@interface ViewController2 :
UIViewController

@end

B的实现文件:

#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 得用代理反向传值的更多相关文章

  1. OC10_代理反向传值

    // // ProtectedDelegate.h // OC10_代理反向传值 // // Created by zhangxueming on 15/6/24. // Copyright (c) ...

  2. iOS 代理反向传值

    在上篇博客 iOS代理协议 中,侧重解析了委托代理协议的概念等,本文将侧重于它们在开发中的应用. 假如我们有一个需求如下:界面A上面有一个button.一个label.从界面A跳转到界面B,在界面B的 ...

  3. iOS-设计模式之代理反向传值

    代理设计模式就是自己的方法自己不实现,让代理对象去实现. 可以让多个类实现一组方法. 委托模式的好处在于: 1.避免子类化带来的过多的子类以及子类与父类的耦合 2.通过委托传递消息机制实现分层解耦 代 ...

  4. 利用协议代理实现导航控制器UINavigationController视图之间的正向传值和反向传值

    实验说明 (1)正向传值:比如A类里地值要传给B类用,就是我们先在A类中声明一个B类对象(当然B类头文件要import过来),然后把A类中得某个 值传递给B类中得某个值(所以需要在B类中先准备一个变量 ...

  5. iOS Block界面反向传值

    在上篇博客 <iOS Block简介> 中,侧重解析了 iOS Block的概念等,本文将侧重于它们在开发中的应用. Block是iOS4.0+ 和Mac OS X 10.6+ 引进的对C ...

  6. IOS Block 反向传值

    1.在需要像上一个界面传值的.h 文件实现代理方法 @property (nonatomic, copy) void(^isOpenHandler)(BOOL) ; 2.在执行操作的时候需要江操作的结 ...

  7. 03-UIKit、VC之间正向反向传值、代理

    目录: 一.正向传值 二.反向传值 三.代理模式 回到顶部 正向传值:就是把第一个界面的值传给第二个界面显示,其简单实现方法 1 首先在第一个界面中要有一个textField输入框,一个按钮butto ...

  8. [ios][swift]使用swift闭包进行viewcontroller反向传值

    闭包参考:http://c.biancheng.net/cpp/html/2285.html   闭包详解 传值参考:http://www.tuicool.com/articles/vy2uUz Sw ...

  9. IOS 学习笔记 2015-04-15 控制器数据反向传值

    // // FirstViewController.h // 控制器数据传递 // // Created by wangtouwang on 15/4/15. // Copyright (c) 201 ...

随机推荐

  1. html用过标签记录

    nowrap="nowrap" //用于列表中td不许换行 maxlength="100" //用于输入框的长度限制 colspan="2" ...

  2. Json解析与Gson解析

    本文主要介绍json最原始的解析与google提供的gson工具类解析 ①json解析 /** * 普通的json解析 * @param s * @throws JSONException */ pr ...

  3. java攻城师之路--复习java web之request_respone

    Servlet技术 两条主线1.HTTP协议 2.Servlet生命周期 init() 方法中参数 ServletConfig 对象使用通过ServletConfig 获得 ServletContex ...

  4. c++将bool变量以文字形式打印

    #include <iostream> // std::cout, std::boolalpha, std::noboolalpha int main () { bool b = true ...

  5. CSS之float浮动

    CSS理解之float浮动 首先我们看看W3C给出的关于 float 的说明: 参考资料   MDN   W3C

  6. shell脚本网络流量实时查看

    Linux网络流量实时查看脚本,Centos默认没有自带流量查看工具,通过网上的资料做了一些修改 #!/bin/bash # Author: Ca0gu0 # Script Name: idev.sh ...

  7. seam的定时轮巡

    青岛的项目要做一个功能,每天凌晨2点的时候保存一次设备数据,这个就要求项目能够间隔24小时每天去做这个事,是一个自主轮巡. seam框架正好提供了这个功能,@Expiration指定开始时间,@Int ...

  8. Mysql 在Linux下的安装

    1.获取mysql源码 wget http://dev.mysql.com/get/Downloads/MySQL-5.5/mysql-5.5.49.tar.gz 3.添加mysql用户和用户组,创建 ...

  9. UNIX C XSI_IPC对象、共享内存

    1.创建IPC对象 #include <sys/ipc.h> key_t ftok(const char* pathname,int proj_id); 成功返回可用于创建或获取IPC的键 ...

  10. ES6 基础学习

    ECMAScript 6 标准入门 一.let和const let命令 let命令,用来声明变量.它的用法类似于var,但是所声明的变量,只在let命令所在的代码块内有效:是块级作用域,且let不允许 ...