In the previous blog iOS Agent protocol, focusing on the concept of principal-agent protocol, this article will focus on their application in the development.
If we have a demand as follows: interface A has a button, a label. From interface a jump to interface B, enter the string in the input box of interface B and display it on the label of interface A. This is a typical example of a reverse value. The core of this example is: "Enter the string in the input box of interface B and display it on the label on interface A." That is: "Interface B Delegate interface a display string, page A is the agent of interface B." The delegate direction agent reverses the value.
So how do we use the proxy design pattern to implement this requirement?
In the program:
1. The tasks to be commissioned are:
1.1 Defining protocols and methods
1.2 Declaring a delegate variable
1.3 Setting up Proxies
1.4 Calling a delegate method from a delegate variable
2. The work that the agent needs to do is:
2.1 Following the agreement
2.2 Implementing a Delegate method
In the BViewController.h:
// defining protocols and methods @protocol Deliverdetegate <nsobject>-(void) SetValue: (NSString *)string; @end @interface Bviewcontroller:uiviewcontroller // declaring a delegate variable ID<DeliverDetegate>b_delegate; @end
In the BVIEWCONTROLLER.M:
@interface Bviewcontroller () <UITextFieldDelegate>*delivertext; @end
-(Ibaction) Deliveraction: (ID) Sender {//invoking a delegate method from a delegate variable//Input Displays the input string and does not enter "not filled in" if(! [_delivertext.text isequaltostring:@""]) {NSLog (@"b send data to a%@", _delivertext.text); //determine if the methods in the agent are implemented to prevent programs that are not implemented by the agent from crashing if([Self. B_delegate respondstoselector: @selector (setValue:)]) {[Self. B_delegate Setvalue:_delivertext.text]; } } Else{NSLog (@"b send data to a%@",@"not filled"); //determine if the methods in the agent are implemented to prevent programs that are not implemented by the agent from crashing if([Self. B_delegate respondstoselector: @selector (setValue:)]) {[Self. B_delegate SetValue:@"not filled"]; }} [Self.navigationcontroller Popviewcontrolleranimated:yes];}
In the AVIEWCONTROLLER.M
#import " AViewController.h " #import " BViewController.h " @interface Aviewcontroller () <DeliverDetegate>*Textlabel; @end
-(Ibaction) Receiveaction: (ID) sender { // Follow protocol BVIEWCONTROLLER*BVC = [[Bviewcontroller alloc]init]; = self ; [Self.navigationcontroller PUSHVIEWCONTROLLER:BVC animated:yes];}
// implement the Delegate method, the SetValue method that is implemented -(void) SetValue: (NSString *)string{ NSLog (@ " A receives the B data%@",string); string ;}
The last of the following:
Output log:
As this article is using Xib, omitted some UI details, attached to the code of this article link: source.
Where to write bad, hope to comment on the guidance. thanked ~
IOS Proxy reverse-pass value