Notification mechanism in IOS programs

Source: Internet
Author: User

Each application has a Nsnotificationcenter instance that assists in communication between different objects, and any object can post notifications to the Notification Center (nsnotication), describing what to do in the notification. Other interested objects can request to receive this notification when a specific notification or specific object is notified.

A notification typically consists of 3 attributes:

Name of the notification

-(NSString *) name;

The publisher of the notification

-(ID) object;

Additional information added to the notification

-(Nsdictionary *) userinfo;

  

The following methods are used to initialize a notification object

  + (Instancetype) Notificationwithname: (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;

Notification hubs (Nsnotificationcenter) provides the appropriate method to register listeners (Observer) to listen for announcements

  -(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: After receiving the notification, this method of callback listener, and the notification object as a parameter passed

AName: Name of the notification. If nil, the listener can receive this notification regardless of the name of the notification

AnObject: Notifies the 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: The names of the notifications

Obj: Notifies the publisher

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

Queue: Determines which operation queue The block executes in, and if nil is passed, the default is performed synchronously in the current Operation queue.

The notification hub does not retain the listener object, and the object that is registered in the notification hub must be unregistered before the object is released. Otherwise, the notification hub will still send a message to the listener when the corresponding notification appears again. The Application Crash Notification Center provides a way to unregister listeners because the corresponding listener object has been released

  -(void) Removeobserver: (ID) observer;

-(void) Removeobserver: (ID) Observer name: (NSString *) AName object: (ID) anobject;

Generally unregister before the listener is destroyed, typically in the following ways:

  -(void) Dealloc {

[Super Dealloc];

[[Nsnotificationcenter Defaultcenter] removeobserver:self];

}

The Uidevice class provides a singleton object that represents the device through which information about the device can be obtained, such as battery level (batterylevel), battery status (batterystate), type of device (model, such as ipod, iphone, etc.) , the device's system (Systemversion) can obtain this single-grain object through [uidevice Currentdevice] Uidevice object will publish some notifications without interruption, the following is the name constant of the notification published by the Uidevice object: Uideviceorientationdidchangenotification //device rotation

  uidevicebatterystatedidchangenotification //battery status

  uidevicebatteryleveldidchangenotification //battery charge

  uideviceproximitystatedidchangenotification //Proximity sensor

Notification mechanism in IOS programs

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.