Using delegates:
Can be thought of as declaring a protocol in a class, and another class as a delegate to implement the method within this protocol.
For example, when creating a table in the iphone, you need to use the UITableView class. However, this class does not know the title of the table, does not know how many parts or rows need to be displayed, and does not know what should be displayed in each row or cell in the table. So it delegates this series of work to you, using a defined protocol: Uitableviewdatesource. When he needs to display the information, it calls the appropriate protocol method.
A uitableviewdelegate protocol is also defined in the UITableView class, which indicates the action that should occur when a row is selected, and it delegates the decision of the action to the user.
1. Informal protocols: informal agreement
It is actually a named class extension (category), only declares a list of methods, but does not implement these methods, it is also known as the Universal Extension (informal category), sometimes called abstract protocols, Similar to the abstract class in CPP).
Anyone should inherit the same root class, such as NSObject. Generic extensions (informal category) are generally defined in the root class.
- Consider the following code, the Nscomparisonmethods extension in the NSObject class:
1 @interface NSObject (nscomparisonmethods) 2 -(BOOL) Isequalto: (IDobject; 3 -(BOOL) Islessthanorequalto: (IDobject; 4 ... 5 @end
(The following translates formal protocol into a proprietary agreement, translating informal protocol into a universal protocol to differentiate)
A general purpose protocol (generic extension) is simply a list of some of the column method names included under an extension, which is advantageous for the archiving and modularity of the methods.
The class that declares the universal protocol does not implement its methods, and the subclass of this class must re-declare the method in the interface of the subclass if it wants to implement the method in the common protocol.
Unlike the private protocol (normal protocol), the compiler does not debug the generic protocol, such as acceptance checks on the protocol or protocol testing.
If a class object accepts a private protocol, it must accept all required messages for the protocol, whether it is compiled or run, and the availability of the message is guaranteed.
However, if an object accepts a common protocol, the object does not need to accept all the messages in the protocol, but is determined by the protocol itself. Message availability is performed at run time, but cannot be guaranteed at compile time.
(@optional directives in OC2.0 are added to replace informal protocol, and there are already some applications in the Uikit class.) )
2. Composite Objects: Combining objects
An object of a class can consist of other class objects, which is also a means of extending the class (in addition to inheritance and class extensions).
About the delegate delegate in OC