Ios has to use proxy to reverse pass the value

Source: Internet
Author: User

Ios has to use proxy to reverse pass the value

Application Scenario: sometimes jump from interface A to interface B, and interface B needs to pass the processing result to A when returning.

Implementation logic: 1. Define A coroutine for transferring values. Interface A has the coroutine attribute and implements the method in the coroutine.

2. Interface B also has this attribute (the proxy requires both of them to have references to the same object), and then obtains the reference pointer of interface A when returning, in addition, specify the call target in B as A, and call the value passing method in B.

Code:

Header file of:


# Import

@ Protocol passValueDelegate

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


@ End


@ Interface ViewController: UIViewController

@ Propertyid PassValueDelegate;

@ End


Implementation file of:


# 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


Header file B:


# Import


@ Interface ViewController2: UIViewController


@ End


B's implementation file:


# 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

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.