应用场景:有时时候从界面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. java.net.URISyntaxException: Illegal character in query

    java使用httpclient爬取一个网站的时候,请求:String url3="http://sh.58.com/ershoufang/33562546149042x.shtml?amp ...

  2. 通过HTTP协议实时获取微信聊天记录

    第一步:登陆 1.get访问微信首页https://wx.qq.com 提供session.headers 用途:获取cookie 后续访问必须带session.headers.cookie这三个参数 ...

  3. mysql有关时间是问题

     mysql中有关时间的类型 date/datetime/time/timestamp/year date:表示日期的类型,格式为:“yyyy-MM-dd” dateTime:表示日期时间的类型,格式 ...

  4. 关于Python中的classmethod

    Python 中的 classmethod classmethod: 作用是直接将自己的类对象,传给类方法. 一.classmethod 1)不用classmethod的时候 你的代码可能是这样写的, ...

  5. Gradle与Makefile构建工具的对比

    随着Android Studio的普及,越来越多的Android开发者也要开始了解和学习Gradle这款强大的代码构建工具了.我们在学习和了解一项新事物的时候,最快速的方法往往是与已知的事物进行比较, ...

  6. SqlServer IsNull 与 NullIf

    ISNULL(check_expression, replacement_value) check_expression 与 replacement_value 数据类型必须一致,如果 check_e ...

  7. B.4 集

    在.NET 3.5之前,框架中根本没有公开集(set)集合.如果要在.NET 2.0中表示集,通常会 使用 Dictionary<,> ,用集的项作为键,用假数据作为值..NET3.5的 ...

  8. idea和eclipse互相导入方法

    1.eclipse maven项目导入idea 只需要在pom.xml文件中加入如下图 为了加载项目里的一些配置文件(例如.xml和.properties文件) 2.idea maven 项目导入ec ...

  9. CentOS 7安装JDK 1.8

    1. 首先查看当前Linux系统是否安装Java ``` rpm -qa | grep java ``` 2. 如果列表显示有,则使用命令将其卸载 rpm -e --nodeps 要卸载的软件名 或 ...

  10. java常见知识

    在JSP页面获取当前项目名称的方法: 方法1: <%= this.getServletContext().getContextPath() %> 方法2: 使用EL表达式 ${pageCo ...