"iOS Notes" delegation

Source: Internet
Author: User

Delegation

Delegation is a simple and powerful the pattern in which one object in a program acts on behalf of, or in coordination with, a Nother object. The delegating object keeps a reference to the other object-the Delegate-and at the appropriate time sends a message to it . The message informs the delegate of an event, the delegating object is about to handle or have just handled. The delegate respond to the message by updating the appearance or state of itself or other objects in the application, And in some cases it can return a value this affects how an impending event is handled. The main value of delegation is the it allows-easily customize the behavior of several objects in one central Obje Ct.

Delegate is a simple and powerful pattern. In this mode, an object in the application represents another object, or works in coordination with another object. The authorization object retains a reference to another object (the delegate object) and sends information to the delegate object at the appropriate time. This information tells the delegate object of the event that the authorization object is about to be processed or that an event has just been processed. A delegate object might respond to this information by updating the appearance or state of itself or other objects in the application, and in some cases it returns a value that reflects how the event to be handled is handled. The principal value of a delegate is that it allows you to easily define the behavior of several objects in a central object.

The delegate pattern is not only common for existing framework classes, but can also be applied between two custom objects of an application. A common design is to use delegates as a means to allow the child view Controller to communicate certain values (typically the values entered by the user) to the parent view controller.

Delegation and the Cocoa frameworks delegation and Coco frame

The delegating object is typically a framework object, and the delegate is typically a custom controller object. In a managed memory environment, the delegating object maintains a weak reference to its delegate; In a garbage-collected environment, the receiver maintains a strong reference to its delegate. Examples of delegation abound in the Foundation, UIKit, AppKit, and other Cocoa and Cocoa Touch frameworks.

Delegation object is a typical frame object, and delegate is a typical controller object. In a managed memory environment, delegating object keeps a weak reference to its delegate; In a garbage collection environment, the recipient maintains a strong application to its delegate. Examples of delegation in the framework, Uikit,appkit, other Cocoa,cocoa touch frameworks.

An example of a delegating object was an instance of the NSWindow class of the AppKit framework. NSWindow Declares a protocol, among whose methods is windowShouldClose: . When a user clicks the Close box in a window, the Window object sends to its windowShouldClose: delegate to ask it to confirm the Closu Re of the window. The delegate returns a Boolean value, thereby controlling the behavior of the Window object.

An example of a delegating object is an instance of the Nswindow class. Nswindow declares a protocol in which there is a method windowshouldclose. When a user clicks the Close button, the Window object sends Windowshouldclose to its delegate to confirm that the window is closed. Delegate returns a bool value that controls the behavior of the Window object.

Delegation and notifications delegation and notifications

The delegate of the most Cocoa framework classes is automatically registered as an observer of notifications the "by the Del" Egating object. The delegate need only implement a notification method declared by the framework class to receive a particular Notificatio N message. Following the example above, a Window object posts an to NSWindowWillCloseNotification observers but sends a windowShouldClose: message to its delegate.

Most delegate of the Cocoa Framework class are automatically registered as a notification observer for delegating object. Delegates only need to implement notification methods declared by the framework class to receive a specific notification message. In the example above, a Window object is released with a nswindowwillclosenotification to the observer, but sends a windowshouldclose: message to its delegate.

Data source DataSource

A data source is almost identical to a delegate. The difference is in the relationship with the delegating object. Instead of being delegated control of the user interface, a data source is delegated control of data. The delegating object, typically a view object such as a table view, holds a reference to its data source and occasionally asks it for the data it should display. A data source, like a delegate, must adopt a protocol and implement at minimum the required methods of that protocol. Data sources is responsible for managing the memory of the model objects they give to the delegating view.

The data source is almost the same as delegate, and the difference is in the relationship to delegating object. Unlike the delegated control of the UI, the data source is controlled by the data. Delegating object is typically a view object, such as a table view, that holds a reference to the data source and often asks the data source if it should be displayed. A data source, like a deleate, must inherit a protocol and implement at least some of the methods that the protocol must always have. The data source is responsible for managing the in-memory data model.

Prerequisite Articles
    • Class definition
Related articles
    • Notification
    • Protocol
    • Controller Object
Definitive DiscussionDelegates and Data Sources in Concepts in OBJECTIVE-C programming Sample Code Projects
    • TableView Fundamentals for IOS

"iOS Notes" delegation

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.