What is an agent?
Apple's official documentation gives a clear explanation:
Delegation is a-simple and powerful-pattern in which one object-a program acts on behalf of, or in coordination with, Another object. The delegating object keeps a reference to the other object-the Delegate-and at the appropriate time sends a message to it . The message informs the delegate of an event, the delegating object is about to handle or have just handled. The delegate respond to the message by updating the appearance or state of itself or other objects in the application, And in some cases it can return a value this affects how an impending event is handled. The main value of delegation is the it allows-easily customize the behavior of several objects in one central Obje Ct.
It is a simple and powerful design pattern for an object to "represent" another object and interact with other objects in the program. The main object (in this case, delegating object) maintains a reference to a proxy (delegate) and sends a message to the agent at the appropriate time. This message informs the "agent" that the main object is about to be processed or that an event has been processed. The agent can respond to the message of the event sent by the main object by updating the UI interface or other state of the object. Or, in some cases, return a value to affect how other upcoming events should be handled. The main value of the agent is that it allows you to easily customize the behavior of various objects. Note that the agent here is a noun, which is itself an object that is specifically representative of the object being proxied to deal with other objects in the program.
The Protocol (protocol) can only define a common set of interfaces, but cannot provide a specific implementation method. That is, it only tells you what to do, but specifically how to do it, it does not care.
When a class is going to use a certain protocol (protocol), it must obey the protocol. For example, there are some necessary ways to implement, you do not implement, then the compiler will report a warning to remind you not to abide by the XX protocol. Note that I'm talking about a warning, not a mistake. Yes, even if you do not implement the "need to achieve" method, the program can also run, but a few more warnings.
The role of Protocol (protocol):
1. Define a common set of interfaces (public)
2. Principal agent (Delegate) value:
It is a design pattern in itself, it means entrusting someone else to do something.
For example: The value of two classes, Class A called the method of Class B, class B encountered a problem in the execution of the notification Class A, this time we need to use the agent (Delegate).
Another example: the controller and Controller (controllers) between the value of the transfer, from C1 to C2, and then return from C2 to C1 when you need to notify C1 update UI or do other things, this time we use the proxy (Delegate) value.
I. Define a common set of interfaces (public)
First create a new protocol file:
Fill in the protocol file name and file type (select protocol):
ProtocolDelegate.h Code (Protocol does not generate. m files):
#import @protocol // methods that must be implemented @required-(void// Optional method of implementation @optional-(void) Other; -(void) other2; -(void@end
In the class that needs to use the protocol, import its header file:
#import "ViewController.h" #import "ProtocolDelegate.h"
I chose the entry file here.
Remember to abide by the agreement:
@interface Viewcontroller () @end
A warning is reported because one of the defined protocols is a method that must be implemented, and we do not implement:
After implementing a method that must be implemented, the compiler does not report a warning:
As with other alternative methods, you can either choose to implement them, or none of them can be implemented.
Second, the principal agent (Delegate) transmission value
On the storyboard, first make a good interface, such as:
New Controllerb:
Set the class of the B interface to Viewcontrollerb:
The following release of the main class file code, I wrote a note inside, you should be able to read. I will put the demo at the end of this article if I don't understand it.
VIEWCONTROLLER.M file:
1 #import "ViewController.h"2 #import "ProtocolDelegate.h"3 #import "ViewControllerB.h"4 @interfaceViewcontroller ()@end5 @implementationViewcontroller6- (void) Prepareforsegue: (Uistoryboardsegue *) Segue Sender: (ID) Sender7 {8Viewcontrollerb *VC =Segue.destinationviewcontroller;9 [VC setdelegate:self];Ten } One //this implements the Protocol method of the B controller A- (void) Sendvalue: (NSString *) Value - { -Uialertview *alertview = [[Uialertview alloc] Initwithtitle:@"Success"Message:valueDelegate: Nil Cancelbuttontitle:@"Determine"Otherbuttontitles:nil, nil]; the [Alertview show]; - } -- (void) Error - { + } - @end
ViewControllerB.h file:
1 #import //create a new protocol in which the name of the agreement is typically "class name +delegate"2 @protocolViewcontrollerbdelegate//Proxy Value-Transfer method3- (void) Sendvalue: (NSString *) value;4 5 @end6 7 @interfaceViewcontrollerb:uiviewcontroller8 9 //principal agent, proxy generally use weak references (weak)Ten@property (weak, nonatomic)ID Delegate; One A @end
VIEWCONTROLLERB.M file:
1 #import "ViewControllerB.h"2 @interfaceViewcontrollerb ()3@property (Strong, nonatomic) iboutlet Uitextfield *TextField;4 @end5 @implementationViewcontrollerb6-(Ibaction) Backaction: (ID) Sender7 {8 if([_delegate respondstoselector: @selector (sendvalue:)]) {//if the protocol responds to the Sendvalue: Method9[_delegate Sendvalue:_textfield.text];//notification execution protocol methodTen } One [Self.navigationcontroller Popviewcontrolleranimated:yes]; A } - @end
Finish the effect:
Summary:
When you need to define a common set of interfaces, the implementation method can be different when you can use the Protocol protocol.
When you need to pass values between classes and classes, you can also use the proxy design pattern to pass values based on the Protocol protocol.
0113--Proxy Mode