The Cocoa Touch framework uses a large number of delegation. According to Apple's documents, delegate is a form of delegation mode adopted by the Cocoa Touch framework. The instance code download portal understands the delegate. The required preparation (1) Objective-C protocol is similar to the abstract class of C ++ and the JAVA interface. The specific definition is as follows: [cpp] @ protocol MyButtonDelegate <NSObject> @ optional-(void) didPressButton; @ end @ protocol is the protocol keyword, and MyButtonDelegate is the protocol name, didPressButton is the method in the Protocol. (2) The id type can be understood as a pointer to any object, which is defined as: [cpp] typedef struct objc_class * Class; typedef struct objc_object {Class isa ;} * id; (3) there is no delegate mode in the adapter mode. But there is an adapter mode, the adapter mode can be understood as this, if we went to Hong Kong, to connect my computer to power, we found that some plug boards can not be plugged in (Hong Kong uses a British plug ), you can only insert one converter to connect the computer to the converter. This is the adapter mode in our daily life. Most of the delegation modes are the functions of the implemented Object Adapter. (4) We want to implement a self-built component similar to the UIButton function. First, we inherited Mybutton from UIView. At this time, we encountered a problem. We don't know who will use Mybutton in the future, but we know that every button that uses mybutton needs to be pressed, get a message to inform the Adaptee object that uses mybutton, and mybuttton is pressed. In this case, our adaptation <Target> is as follows: [cpp] @ protocol MyButtonDelegate <NSObject> @ optional-(void) didPressButton; @ end my Adapter is Mybutton, it uses [cpp] @ property (nonatomic, assign) id <MyButtonDelegate> delegate; [cpp] if (delegate & [delegate respondsToSelector: @ selector (didPressButton)]) {[delegate initialize mselector: @ selector (didPressButton)];} to call Adaptee. Adaptee can be any object. In this example, RootViewController (implemented <MyButtonD Elegate> Protocol) (5) the delegation mode provides an in-depth understanding of the implementation of the delegation mode. It can also be implemented through Block, but it is only suitable for code that is executed in one-time callback.