iOS 代理反向传值

应用场景:有时时候从界面A跳转到界面B,界面B在返回的时候需要将处理的结果传递给A.

实现思路:1,定义一个负责传值的协义,界面A拥有该协义属性,并实现该协义中的方法

2,界面B也拥有该协义属性(代理要求两者都具有相同对象的引用 ),然后在返回的时候获取界面A的引用指针,并且指定B中协义的调用目标为A,调用协义中的传值方法.

具体代码:

A的头文件 :

#import<UIKit/UIKit.h>

@protocolpassValueDelegate <NSObject>

-(void) setValue:(NSString*)param;

@end

@interfaceViewController :UIViewController

@propertyid<passValueDelegate> passValueDelegate;

@end

A的实现文件 :

#import"ViewController.h"

#import"ViewController2.h"

@interfaceViewController()

{

}

@end

@implementationViewController

- (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:selfaction:@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>

@interfaceViewController2 :UIViewController

@end

B的实现文件:

#import"ViewController2.h"

#import"ViewController.h"

@interfaceViewController2()

@propertyid<passValueDelegate> passValueDelegate;

@end

@implementationViewController2

- (void)viewDidLoad

{

    [superviewDidLoad];

   UIButton*btn =[UIButton buttonWithType:UIButtonTypeRoundedRect];

btn.frame=CGRectMake(100,100,200,40);

    [btn setTitle:@"back"forState:UIControlStateNormal];

    [btnaddTarget:selfaction:@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

{

    [superdidReceiveMemoryWarning];

   // Dispose of any resources that can be recreated.

}

@end


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值