How to Understand the IOS delegate Design Mode

Source: Internet
Author: User

Reprinted from: http://leopard168.blog.163.com/blog/static/168471844201111411729597/

The IOS programming language is objective-C (OC. Speaking of the characteristics of OC, it is often said that OC does not support multi-inheritance. But delegate makes up for this defect. With delegate, objects can be declared to comply with multiple protocols. This solves the problem of multi-inheritance.

Delegate, also known as a delegate or proxy, is a design pattern. To learn about iOS development, you need to have a deep understanding of the delegate usage. Apple has a clear description of delegate. Http://developer.apple.com/library/ios/#documentation/General/Conceptual/DevPedia-CocoaCore/Delegation.html
But it is still abstract.

Purpose of delegate:

Used to change or control other objects.

Delegate definition:

(1) delegate is an object of the ID type (anonymous type: anonymous type );

(2) The delegate reference is generally an instance variable (instance variable) and is named delegate;

(3) The access mode (accessors pattern) is used in the delegate)

Delegate message Name:

A message sent to a delegate usually contains one of the (shocould, Will, did.

Shocould: expecting delegate to return a value;

WILL: indicates the things to be done before a change occurs;

Did: indicates what to do after a change occurs.

Many classes of cocoa touh use delgete to varying degrees. For example, nstextfield and nstableview. Data source is also used for nstableview.

In fact, data source is also a type of delegation. Data Source reduces the coupling between views and models. Nsappplication implements dozens of delegate methods.

Delegate considerations:

Delegate is an ID-type object, which also has the issue of creation and release. For data source, data Souce can be released only when users of data source (such as table view) are released. Otherwise, crash will appear. Because the data is lost when table view obtains data.

Delegate can be used in multiple scenarios, such as data interaction between objects and behavior interaction between different views. There are still many ways to implement data interaction. Delegate is especially suitable for behavior interaction between views.

This section describes how to use uiactionsheet and uialertview.

Uiactionsheet * actionsheet = [[uiactionsheet alloc]

Initwithtile: @ "Are you sure ?"

Delegate: Self

Cancelbuttontitle: @ "No way !"

Destructivebuttontitle: @ "yes, I'm sure !"

Otherbuttontitles: Nil];

 

Here, pay special attention to the use of delegate: Self. It indicates that delegate will receive a notification when the actionsheet button is pressed. More specifically, the delegate actionsheet: diddismisswithbuttonindex: method will be called. Pass self as the delegate parameter to this method to ensure that actionsheet: diddismisswithbuttonindex:
Called.

As the name suggests, cancelbutton is the cancel button. Correspondingly, destructivebutton is the OK button. With the delegate: Self setting, we can determine whether the operation is canceled or determined by the user selected in the actionsheet: diddismisswithbuttonindex: method.

If you do not need to differentiate which button to press, you can directly use uialertview. The example is as follows:

Uialertview * Alert = [[uialertview alloc]

Initwithtitle: @ "something was done"

Message: @ "everything is OK"

Delegate: Nil

Cancelbuttontitle: @ "cancel"

Otherbuttontitles: Nil];

Each view has its own delegate. Similarly, uialertview also has its own delegate. If we want to know when the user closes the alertview prompt box or want to determine which button the user presses, you can use delegate: Self. In fact, alertview only provides a button to the user. You do not need to determine which button the user is pressing, so declare delegate: nil.

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.