Notification Mechanism in ios programs and ios program notification mechanism

Source: Internet
Author: User
Tags notification center

Notification Mechanism in ios programs and ios program notification mechanism

Each application has an nsicationicationcenter instance to facilitate communication between different objects. Any object can publish a notification (NSNotication) to the notification center to describe what it does. Other interested objects can apply to receive the notification when a specific notification or a specific object sends the notification.

A notification generally contains three attributes:

// Notification name

-(NSString *) name;

// Notification publisher

-(Id) object;

// Additional Notification Information

-(NSDictionary *) userinfo;

  

You can initialize a notification object using the following methods:

  + (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;

 

 

The notification center (nsicationicationcenter) provides the corresponding method to register the listener (Observer) to listen for the release of notifications.

  -(Void) addObserver :( id) observer selector :( SEL) aSelector name :( NSString *) aName object :( id) anObject;

Parameter resolution: observer: Listener, that is, who wants to receive this notification

ASelector: When a notification is received, this method of the listener is called back and the notification object is passed in as a parameter.

AName: the 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;

Parameter resolution: 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 in the current operation queue by default.

The notification center does not retain the listener object. If you have registered an object in the notification center, you must cancel the registration before the object is released. Otherwise, when the corresponding notification appears again, the notification center will still send a message to the listener. The listener object has been released, which may cause application crash. The notification center provides a method to cancel listener registration.

  -(Void) removeObserver :( id) observer;

-(Void) removeObserver :( id) observer name :( NSString *) aName object :( id) anObject;

Generally, the registration is canceled before the listener is destroyed. The following method is generally used:

  -(Void) dealloc {

[Super dealloc];

[[Nsicationcenter center defacenter center] removeObserver: self];

}

The UIDevice class provides a singleton object, which represents a device. You can obtain device-related information, such as batteryLevel and batteryState), the device type (model, such as iPod, iPhone, etc.), the device's system (systemVersion) can be obtained through [UIDevice currentDevice] This single object UIDevice object will continuously publish some notifications, the following are the name constants of notifications published by the UIDevice object:UIDeviceOrientationDidChangeNotification// Device Rotation

  UIDeviceBatteryStateDidChangeNotification// Battery status

  UIDeviceBatteryLevelDidChangeNotification// Battery power

  UIDeviceProximityStateDidChangeNotification// Close-range sensor

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.