Preface: Because OBJECT-C does not support multiple inheritance, it is often replaced with protocol (protocol). 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.
I will put this demo at the end of this article, if necessary, you can download, thank you.
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):
1 #import @protocolProtocoldelegate//methods that must be implemented2 @required3- (void) error;4 5 //optional methods of implementation6 @optional7- (void) Other;8- (void) Other2;9- (void) Other3;Ten One @end
In the class that needs to use the protocol, import its header file:
1 #import " ViewController.h " 2 #import " ProtocolDelegate.h "
I chose the entry file here.
Remember to abide by the agreement:
@interface @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.
Demo Test through the environment:
Demo: gcprotocol&delegate
Protocol protocol and principal agent (Delegate) Transfer value