Notification nsnotication & notification center nsnoticationcenter

Source: Internet
Author: User
Tags notification center

@ Font-face {font-family: "wingdings" ;}@ font-face {font-family: "" ;}@ font-face {font-family: "" ;}@ font-face {font-family: "@ ";}@ font-face {font-family: "Cambria" ;}p. msonormal, Li. msonormal, Div. msonormal {margin: 0 cm 0 cm 0.0001pt; text-align: justify; font-size: 12pt; font-family: Cambria;} p. msolistparagraph, Li. msolistparagraph, Div. msolistparagraph {margin: 0 cm 0 cm 0.0001pt; text-align: justify; text-indent: 21pt; font-size: 12pt; font-family: Cambria ;}. msochpdefault {font-family: Cambria;} Div. wordsection1 {page: wordsection1 ;}ol {margin-bottom: 0 cm ;}ul {margin-bottom: 0 cm ;}

Notification center (nsicationicationcenter)

Each application has a notification center (nsnoticationcenter) instance to assist 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 the notification when a specific notification is published (or when a specific object is published ).

 

Notification nsnotification

A complete notification generally contains three attributes:

1>-(nsstring *) Name; // notification name

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

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

Initializes a notification object (nsnotification ).

1> + (instancetype) notifactionwithname :( nsstring *) aname object :( ID) anobject;

2> + (instancetype) notificationwithname :( nsstring *) aname object :( ID) anobject userinfo :( nsdictionary *) auserinfo;

3>-(instancetype) initwithname :( nsstring *) Name object :( ID) object userinfo :( nsdictionary *) userinfo;

Release notification

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

1>-(void) postnotification :( nsnotification *) notification;

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

2>-(void) postnotificationname :( nsstring *) aname object :( ID) anoject;

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

3>-(void) postnotificationname :( nsstring *) aname object :( ID) anobject userinfo :( nsdictionary *) auserinfo;

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

Register notification Monitor

The notification center nsicationicationcenter provides a method to register a listener's monitor observer.

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

Observer: Monitor, that is, who wants to receive this notification

Aselector: After receiving the notification, callback the monitor method and pass the notification object as a parameter.

Aname: the name of the notification. If it is nil, the monitor will receive this notification regardless of the notification name.

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

Register notification Monitor

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

Name: Notification name

OBJ: Notification publisher

Block: When a notification is received, the block is called back.

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

Cancel registration notification Monitor

The notification center does not retain the listener object. The object registered in the notification center must be canceled before the object is released. Otherwise, when the corresponding notification appears again, the notification center will still send a message to the listener. Because the listener object has been released, the application may crash.

 

The notification center provides methods to cancel listener registration.

-(Void) removeobserver :( ID) observer;

-(Void) removeobserver :( ID) Observer name :( nsstring *) aname object :( ID) anobject;

 

Generally, cancel registration before the listener is destroyed (for example, add the following code to the listener ):

-(Void) dealloc {

// [Super dealloc]; this sentence needs to be called in non-Arc

[[Nsicationcenter center defacenter center] removeobserver: Self];

}

 

Uidevice notification

The uidevice class provides a single object, which represents a device and can be used to obtain device-related information, such as batterylevel and batterystate), device type (model, such as Ipod, iPhone, etc.), device system (systemversion)

 

You can use [uidevice currentdevice] to obtain this single object.

 

The uidevice object will continuously publish some notifications. The following are the name constants of notifications published by the uidevice object:

Uideviceorientationdidchangenotification // rotate the device

Uidevicebatterystatedidchangenotification // The Battery status changes.

Uidevicebatteryleveldidchangenotification // battery power change

Uideviceproximitystatedidchangenotification // close sensor (for example, the device is close to the user's face)

 

Keyboard notification

When the system sends a keyboard notification, additional information (dictionary) related to the keyboard is attached. The common keys in the dictionary are as follows:

Uikeyboardframebeginuserinfokey // frame at the beginning of the keyboard

Uikeyboardframeenduserinfokey // The final frame of the keyboard (after the animation is executed)

Uikeyboardanimationdurationuserinfokey // time of keyboard Animation

Uikeyboardanimationcurveuserinfokey // execution speed of the keyboard animation (fast and fast)

 

Notification and proxy Selection

Commonalities

Communication between objects can be completed using notifications and Proxies

(For example, object a tells object d what happened and object a passes data to object d)

 

Differences

Proxy: one-to-one relationship (one object can only tell the other object what happened)

Notification: many-to-many relationship (one object can tell n objects what happened, And one object can know what happened to n objects)

 

Notification nsnotication & notification center nsnoticationcenter

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.