Delegate and Protocol

Source: Internet
Author: User
  1. We send a message to the object to initiate a specific action. When the object sends some changes, it notifies us through the callback function. When a specific event occurs, the object calls the corresponding callback function to trigger the business logic.
  2. The callback function is implemented through the so-called delegation.

Delegate ------- benefits:

It separates the logic processing of the program from the uiapplication more concisely.

It avoids programmers from directly deriving subclasses from uiapplication. Imagine how painful it is to manage the lifecycle and system events of an application by deriving subclasses from uiapplication.


In the "delegate mode", two objects are generally involved in processing the same request. The so-called request in the iPhone is the application's lifecycle and system events, the object that receives the request delegates the request to another object for processing. In the same iPhone, The uiapplication delegates the request to uiapplicationdelegate after receiving the lifecycle and system events.


The reason why the iPhone does not use the inheritance system but uses the delegated mode for aggregation is that the iPhone framework is diversified, which makes inheritance unable to ensure the consistency of sub-class behaviors, the delegated mode is precisely designed to make up for the disadvantages of inheritance.


Each project has a uiapplication object to process the lifecycle and system events of the application. The main () function initializes the uiapplication of the application through uiapplicationmain, if you want to operate the uiapplication of an application, you can only use [uiapplication sharedapplication] To obtain the reference of the uiapplication. This method returns a globally unique uiapplication object to the reader.


Likewise, you can use the following code to obtain the application's delegate object: uiapplicationdelegate * mydelegate = [[uiapplication sharedapplication] Delegate];


Protocol:

Object-C does not have many inheritance. So we need to avoid making an object that can be used in any way (super class monster, huge
, Super, waste) a super object itself denies the concept and meaning of object-oriented. To make the code more concise and organized, you can separate some of the duties.

The Protocol itself has no specific implementation. Only some interfaces that can be implemented by other classes are specified.

Delegate is always defined as assign @ Property

  1. @ Interface uitextfield
  2. @ Property (assign) ID <uitextfielddelegate> delegate;
  3. @ End

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.