ios 得用代理反向傳值

來源:互聯網
上載者:User

ios 得用代理反向傳值

應用情境:有時時候從介面A跳轉到介面B,介面B在返回的時候需要將處理的結果傳遞給A.

實現思路:1,定義一個負責傳值的協義,介面A擁有該協義屬性,並實現該協義中的方法

2,介面B也擁有該協義屬性(代理要求兩者都具有相同對象的引用 ),然後在返回的時候擷取介面A的引用指標,並且指定B中協義的調用目標為A,調用協義中的傳值方法.

具體代碼:

A的標頭檔 :


#import

@protocol passValueDelegate

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


@end


@interface ViewController :UIViewController

@propertyid passValueDelegate;

@end


A的實現檔案 :


#import "ViewController.h"

#import "ViewController2.h"



@interfaceViewController ()

{


}


@end



@implementation ViewController


- (void)viewDidLoad

{

[superviewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

UIButton *btn =[UIButtonbuttonWithType:UIButtonTypeRoundedRect];

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

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

[btn addTarget:selfaction:@selector(btn)forControlEvents:UIControlEventTouchUpInside];

[self.viewaddSubview:btn];

self.view.backgroundColor=[UIColorredColor];

}


-(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


@interface ViewController2 :UIViewController


@end


B的實現檔案:


#import "ViewController2.h"

#import "ViewController.h"


@interfaceViewController2 ()

@propertyid passValueDelegate;

@end


@implementation ViewController2


- (void)viewDidLoad

{

[superviewDidLoad];

UIButton *btn =[UIButtonbuttonWithType:UIButtonTypeRoundedRect];

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

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

[btn addTarget:selfaction:@selector(btn)forControlEvents:UIControlEventTouchUpInside];

[self.viewaddSubview:btn];

}

-(void)btn

{

// NSLog(@"%@",self.navigationController.childViewControllers[0]);

self.passValueDelegate=self.navigationController.childViewControllers[0];

[self.navigationControllerpopToRootViewControllerAnimated:YES];

[self.passValueDelegatesetValue:@"123456789"];

}




- (void)didReceiveMemoryWarning

{

[superdidReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}


@end

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.