Dark Horse programmer-5. Notification)

Source: Internet
Author: User

A mechanism recently learned during notification is used only when the content of the text box is changed. It is not very familiar, but useful. It mainly serves between M and C, the preceding delegate mainly serves between m and V.


Notifacation is a very good mechanism that can broadcast messages. In particular, if you want to transmit a wealth of information, you may not expect anyone to care about the message.

A notification can be used to send any message, or even contain a userinfo dictionary or a subclass of nsnotifacation. The unique feature of a notification is that the sender and receiver do not need to know each other. In this way, messages can be transmitted between very loosely coupled modules. Remember, this message transmission mechanism is unidirectional and cannot be used as a receiver to reply to messages.

Each application has an nsicationicationcenter instance dedicated to assisting in message communication between different objects.

Any object can publish a notification (nsnotification) to the notification center to describe what it is doing. Other Objects of interest (observer) can apply to receive this notification when a specific notification is published (or when a specific object is published!

  


The notification center (nsicationicationcenter) provides a method to register an listener (observer) that listens to notifications)

-(Void) addobserver :( ID) Observer selector :( SEL) aselector name :( nsstring *) aname object :( ID) anobject;

// Observer: Listener, that is, who wants to receive this notification

// Aselector: callback the method of the listener after receiving the notification, and pass the notification object as a parameter.

// Aname: name of the notification. If it is nil, the listener will receive the notification no matter what the notification name is.

// Anobject: Notification publisher. If both anobject and aname are nil, the listener receives all notifications.

-(ID) addobserverforname :( nsstring *) Name object :( ID) OBJ queue :( nsoperationqueue *) queue usingblock :( void (^) (nsnotification * Note) block;

// Name: Notification name

// OBJ: Notification publisher

// Block: When a corresponding notification is received, this callback is triggered.

// Blockqueue: determines the operation queue in which the block is executed. If nil is passed, it is executed synchronously in the current operation queue by default.

  

Notification

A complete notification generally contains three attributes:

-(Nsstring *) Name; // notification name

-(ID) object; // notification publisher (who wants to publish a notification)

-(Nsdictionary *) userinfo; // some additional information (the information that the notification publisher transmits to the notification recipient)

Initializes an nsnotification object.

+ (Instancetype) icationicationwithname :( nsstring *) aname object :( ID) anobject;

+ (Instancetype) notificationwithname :( nsstring *) aname object :( ID) anobject userinfo :( nsdictionary *) auserinfo;

-(Instancetype) initwithname :( nsstring *) Name object :( ID) object userinfo :( nsdictionary *) userinfo;

  

Release notification

The notification center (nsicationicationcenter) provides methods to help publish notifications.

Publish a notification. You can set the notification name, publisher, and additional information in the notification object.

-(Void) postnotification :( nsnotification *) notification;

Publish a notification named aname. anobject is the publisher of the notification.

-(Void) postnotificationname :( nsstring *) aname object :( ID) anobject;

Publish a notification named aname. anobject is the publisher of the notification, and auserinfo is the additional information.

-(Void) postnotificationname :( nsstring *) aname object :( ID) anobject userinfo :( nsdictionary *) auserinfo;



Dark Horse programmer-5. 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.