IOS design mode-delegation Mode

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.