iOS has to reverse the value by proxy

Source: Internet
Author: User

Application scenario: Sometimes from interface a jump to interface B, interface B in return to the need to pass the results of processing to a.

Implementation ideas: 1, define a responsible for the value of the covariance, interface A has the semantic attributes, and implement the method of the covariance

2, Interface B also has this semantic attribute (the proxy requires both have a reference to the same object), then gets the reference pointer of interface A when it returns, and specifies that the calling target of the "B" is a, calling the method of value in the covariance.

Specific code:

A's header file:


#import <UIKit/UIKit.h>

@protocol passvaluedelegate <nsobject>

-(void) SetValue: (nsstring *) param;


@end


@interface Viewcontroller: Uiviewcontroller

@propertyID<passvaluedelegate> passvaluedelegate;

@end


Implementation file for a:


#import "ViewController.h"

#import "ViewController2.h"



@interfaceviewcontroller ()

{


}


@end



@implementation Viewcontroller


-(void) viewdidload

{

[superviewdidload];

Additional setup after loading the view, typically from a nib.

UIButton *btn =[UIButtonbuttonwithtype:uibuttontyperoundedrect];

Btn. frame=CGRectMake(+,+, +);

    [btn settitle: @ "btn" Forstate: uicontrolstatenormal";

    [btn addtarget: self Action: @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, can be recreated.

}


@end


B's header file:


#import <UIKit/UIKit.h>


@interface ViewController2:Uiviewcontroller


@end


Implementation file of B:


#import "ViewController2.h"

#import "ViewController.h"


@interfaceViewController2 ()

@property ID<passvaluedelegate> passvaluedelegate;

@end


@implementation ViewController2


-(void) viewdidload

{

[superviewdidload];

UIButton *btn =[UIButtonbuttonwithtype:uibuttontyperoundedrect];

Btn. frame=CGRectMake(+,+, +);

[btn settitle:@ "Back"forstate:uicontrolstatenormal];

[btn addTarget:self action:@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, can be recreated.

}


@end

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.