博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ios 得用代理反向传值
阅读量:3640 次
发布时间:2019-05-21

本文共 2240 字,大约阅读时间需要 7 分钟。

   应用场景:有时时候从界面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.frame=CGRectMake(100, 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.frame=CGRectMake(100, 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.passValueDelegate=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

  

转载地址:http://jfrun.baihongyu.com/

你可能感兴趣的文章
线段数
查看>>
数列操作 — 线段树入门
查看>>
2016 ICPC大连赛区 [Cloned] H - To begin or not to begin
查看>>
2016 ICPC大连赛区 [Cloned] I - Convex
查看>>
2016 ICPC大连赛区 [Cloned]J - Find Small A
查看>>
Openstack 1
查看>>
Mac 怎么通过自带终端连接linux服务器
查看>>
【模式匹配】KMP算法的来龙去脉
查看>>
Openstack2
查看>>
Openstack 3
查看>>
双机互联
查看>>
openstack 4
查看>>
Openstack5
查看>>
root权限删库跑路
查看>>
Raspberry Pi+阿里云函数计算 树莓派实现温湿度传感器机器人实时钉钉群推送
查看>>
python模拟生态系统
查看>>
(mac上python、c++读取txt文件时的问题)python 统计txt文档里面的每个单词出现的个数
查看>>
吴恩达机器学习 8.2 单变量线性回归(Linear Regression with One Variable)
查看>>
吴恩达机器学习 8.5 Octave教程(Octave Tutorial)
查看>>
吴恩达机器学习 8.10 正则化(Regularization)
查看>>