Notification NSNotication & notification center NSNoticationCenter, notication

Source: Internet
Author: User
Tags notification center

Notification NSNotication & notification center NSNoticationCenter, notication

@ 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 executed synchronously by default in the current operation queue.

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)

 

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.