Reprinted: http://www.cnblogs.com/Camier-myNiuer/archive/2013/03/24/Camier.html
This evening, I finally figured out the agreement and commissioned it. In fact, protocols and delegation are inseparable. If there is an agreement, there must be a commission. I have written the Protocol (equivalent to an interface) by myself. Who can delegate the Protocol to implement it. So there must be a class to implement it. This class becomes the delegate object.
In other words, why do we need a protocol. Let me talk about my opinions. Class A needs to complete a specific function, but some parts of this function are in Class B, so Class A writes a protocol and writes several methods in it. Then let the Class B implement the Protocol, that is, in @ interface XXX <protocol name>, and then specify a delegate object for the protocol in Class B. That is, a (an object of Class A). Delegate = self; self refers to this class (B ). The next thing to do is to implement several methods in this protocol in Class B. Now, this function has been implemented in Class B, so Class A can be used directly now. How does a call the methods in this protocol?
We should. h, write an instance of the <protocol name> type or @ property (nonatomic, assign) ID <protocol name> DDD, and then call the method in the Protocol. Here I insert a sentence where DDD actually refers to the class object that implements the protocol. If you are confused, I will give you another sentence. This attribute is used to specify the object that implements the protocol. That's all you need to understand. Write the specific protocol syntax below
Declarative agreement:
@ Protocol name
Method 1;
Method 2;
......
@ End
For example:
@ End
Implement this protocol
For the class implementation, write the protocol name to be implemented in the angle brackets next to the @ Interface Class Name of that class. That is, <protocol name>.
For example:
In this class, how does one specify the delegate object and write an object to the class that declares the protocol. Then, ". Delegate" is used to specify a delegate object.
For example:
After the delegate object is specified, the methods in the Protocol must be implemented in this class.
For example:
The function has been implemented in this class, so now we have to go back to the original class (that is, the class of the write Protocol) to implement the Protocol method.
How to implement it? As mentioned earlier,
First, you need to declare an instance variable of the ID type or declare an attribute, but the variable Declaration of the ID type is somewhat special, because we need to add the protocol name enclosed by Angle brackets after the ID, we should be able to figure it out.
For example:
Declare an instance variable:
Declare an attribute:
In. m
Next, call this method.
For example:
Or
In the end, we are done. I believe you are tired after reading this article. I suggest you read it again. The agreement and delegation will be understood.
I am an academic beginner. If you have any mistakes, please do not hesitate to give me some advice.