Notification Mechanism (concept), notification mechanism concept

Source: Internet
Author: User
Tags notification center

Notification Mechanism (concept), notification mechanism concept

1. notification center)

The notification center is a singleton class created using the following code.

[NSNotificationCenter defaultCenter];

2. Notification (NSNotification)

// A complete notification generally contains three attributes:-(NSString *) name; // notification name-(id) object; // notification publisher (who wants to publish the notification)-(NSDictionary *) userInfo; // some additional information (the information that the notification publisher sends to the notification recipient)
// Initialize a notification object (NSNotification)-(instancetype) initWithName :( NSString *) name object :( id) object userInfo :( NSDictionary *) userInfo; + (instancetype) notifwithname :( NSString *) aName object :( id) anObject; + (instancetype) notifwitwithname :( NSString *) aName object :( id) anObject userInfo :( NSDictionary *) aUserInfo;

3. Release notification

You can call the corresponding method and send a notification through the singleton object in the notification center.

-(Void) postNotification :( NSNotification *) notification; // publish a notification. You can set the notification name, notification publisher, and additional information in the notification object.-(void) posticationicationname :( NSString *) aName object :( id) anObject; // publish a notification named aName. anObject is the publisher of the notification-(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.

4. Receive notifications

There are two items: register the notification listener and cancel the registration notification listener. Because the notification center does not retain (or retain or strongly reference) 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.

1. You can call the corresponding method through the singleton object in the notification center to register the notification listener.

-(Void) addObserver :( id) observer selector :( SEL) aSelector name :( NSString *) aName object :( id) anObject; // observer: Listener, that is, who wants to receive the notification // aSelector: After receiving the notification, callback the listener's method and pass the notification object as a parameter // aName: the notification name. If it is nil, the listener will receive this notification no matter what the notification name is, that is, all the notifications will be received // anObject: the notification publisher. If both anObject and aName are nil, the listener can receive all notifications-(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 corresponding notification is received, the block will be called back. // queue: determines the operation queue in which the block is executed. If nil is passed, it is synchronously executed in the current operation queue by default.

2. You can call the corresponding method through the singleton object in the notification center to cancel registering the notification listener.

-(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]; [[NSNotificationCenter defaultCenter] removeObserver: self];}

5. Notice notes

Generally, the observer mode is not very efficient, and the performance of the notification center is still very good.

1. the listener must be registered. When the object is destroyed, the registration must be canceled.

2. functions that consume a lot of performance, such as map positioning, Bluetooth communication, and accelerators. You can register a listener when the view appears. When the view disappears, the listener is canceled.

3. Notifications are synchronized. After a message is sent, the listening method is executed first, and the subsequent code is executed after the listening method is executed! If the operations in the listening method are time-consuming, you can enable thread execution.

4. Notification and proxy Selection

First, both of them can complete communication and information transmission between objects. Second, notifications can be multi-to-many relationships, but proxies are only one-to-one relationships.

Vi. Supplementary content

1. UIDevice notification

The UIDevice class also provides a singleton object, which represents a device and can be used to obtain device-related information, such as batteryLevel and batteryState), the device type (model, such as iPod, iPhone, etc.), the device system (systemVersion ).

You can use [UIDevice currentDevice] to obtain the singleton object.

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

UIDeviceOrientationDidChangeNotification // The device rotates UIDeviceBatteryStateDidChangeNotification // The batterstate changes to offline // the battery power changes UIDeviceProximityStateDidChangeNotification // The proximity sensor (for example, the device is near)

2, Keyboard notification

We often need to perform some specific operations when the keyboard is popped up or hidden, so we need to listen to the keyboard status. When the keyboard status changes, the system will send some specific notifications.

UIKeyboardWillShowNotification // The UIKeyboardDidShowNotification is displayed on the keyboard. // The UIKeyboardWillHideNotification is displayed on the keyboard. // the keyboard is hidden soon. The keyboard position is about to change. // The keyboard position is changed. size changed

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:

Keyboard // The initial frame of the keyboard // The final frame of the keyboard (after the animation is executed) UIKeyboardAnimationDurationUserInfoKey // The Time of the keyboard animation UIKeyboardAnimationCurveUserInfoKey // The execution speed of the keyboard animation (fast and fast)

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.