Delegate and Notification

Source: Internet
Author: User

Delegate is a kind of agreement, as the name implies, is to entrust others to help themselves to do something. That is, when you do something inconvenient, you can create a delegate, so you can entrust others to help themselves to achieve what method.

I briefly summed up the role of the delegate I used to have two, one is the value of the transfer, one is the event. 1. The so-called value is often used in class B to pass one of their own data or objects to Class A, let a class to show or deal with. (segmented tightly coupled, often used when code is chunked)2. The so-called event is a Class A what happened, the matter to the attention of the person, that is, the object of the delegate, by the entrusted object to consider what should be reflected after the event. (This is often seen, for example, in asynchronous requests, interface events trigger data-layer changes, etc.)3. Using the delegate assignment, this method feels to be able to give itself a complex value without exposing its own attributes, and it is easier to manage the class so that it can be called only when you want someone else to give you a value. (for example, a delegate in TableView (Datesource) is common).

Example:


nsnotification: This is an observer pattern in which one party adds an observer and a notification is given by a party.

First, add the observer to the class you want to listen to:

[[Nsnotificationcenter Defaultcenter] addobserver:self    selector: (SEL) Aselector    Name:aname object: (ID) AnObject];

When the Observer hears the anobject sending a notification named Aname, it calls the Selector method and gets aselector in the UserInfo method.
AnObject indicates who sent the message from.
in general, the methods in selector can be written like this:

-(void) method: (nsnotification:) Sender {nsdictionary *dict = [sender userInfo];}

In other words, the supervisor heard the AnObject message, the name of the message is Aname, at this time Observer call Aselector method, the notification copy to the Local.

Send notification:

[[Nsnotificationcenter Defaultcenter] Postnotificationname:aname object:self];

Notification hubs will differentiate their objects from being interested in these notifications and notifying them. If, in addition to caring for the observer's notification name and the observed object, but also for other objects, then put the outside of the object in the optional dictionary of the notification, or use the method

PostNotificationName:object:userInfo:
Removal Notifications: Removeobserver: and RemoveObserver:name:object:

[[Nsnotificationcenter Defaultcenter] removeobserver:observer name:nil object:self];

Summary: There are many messaging mechanisms in iOS, and how do we choose between different mechanisms? The following are purely personal views:

1. When an interface requires a tone-back interface and is not associated with any other interface, you can consider using block.

2. When many interfaces use the same implementation method, consider using a proxy.

3. Two interfaces do not appear to have any connection, but one party needs to listen to foreign messages at any time, and may consider using notifications.


Delegate and Notification

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.