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