Notification Center (Nsnotificationcenter)

Source: Internet
Author: User
Tags notification center

• Notification mechanism • Mastery • Announcement of notification • • Notification of notifications • Removal of notifications • Notification Center (Nsnotificationcenter) • Each application has a notification hub (Nsnotificationcenter) instance, Dedicated to assisting with message communication between different objects • Any object can publish a notification (nsnotification) to the notification center describing what it is doing. Other objects of interest (Observer) can request to receive this notification when a particular notification is published (or when a particular object is posted) • Notification (nsnotification) · A complete notification typically contains 3 attributes: ø-(NSString *) name; Name of the notification ø-(ID) object; Notifies the publisher (who wants to publish the notice) ø-(nsdictionary *) UserInfo; Some additional information (informing the publisher of the content of the message sent to the notifying recipient)

• Initialize a notification (nsnotification) 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; Release notification

Notification hubs (Nsnotificationcenter) provides the appropriate method to help publish notifications

-(void) Postnotification: (nsnotification *) notification; Ø Publish a notification notification, you can set the name of the notification in the notification object, notify the Publisher, additional information, etc.

-(void) Postnotificationname: (NSString *) AName object: (ID) anobject; Ø Publish a notification called Aname, anobject the publisher of this notification

-(void) Postnotificationname: (NSString *) AName object: (ID) anobject userInfo: (nsdictionary *) Auserinfo; Ø Publish a notice called Aname, AnObject as the publisher of this notice, auserinfo for additional information • Register Notification Listener

Notification hubs (Nsnotificationcenter) provides a way to register a listener for a listening notification (Observer)

-(void) Addobserver: (ID) Observer selector: (SEL) aselector name: (NSString *) AName object: (ID) anobject; Øobserver: Listener, that is, who wants to receive this notification Øaselector: After receiving the notification, callback listener this method, and the notification object as a parameter passed in Øaname: the 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 • Registration Notification listener • (ID) Addobserverforname: (NSString *) Name object: (ID) obj queue: ( Nsoperationqueue *) Queue usingblock: (void (^) (nsnotification *note)) block; Øname: The name of the notification Øobj: Notifies the publisher Øblock: when a corresponding notification is received, the Blockøqueue is called back: determines which operation queue The block executes in, and if nil is passed, the default is performed synchronously in the current Operation queue

• Unregister notification Listener • Notification hubs does not hold (retain) listener objects, objects registered in notification hubs 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 may crash because the listener object has been freed

• Notification hubs provides the appropriate method to unregister listener ø-(void) Removeobserver: (ID) observer; ø-(void) Removeobserver: (ID) Observer name: (NSString *) AName object: (ID) anobject;

• Generally unregister before the listener is destroyed (e.g. by adding the following code to the listener):

-(void) Dealloc {

[Super Dealloc]; This sentence needs to be called in non-arc

[[Nsnotificationcenter Defaultcenter] removeobserver:self];

}

Uidevice informs the Uidevice class that it provides a single-grain object that represents the device through which you can obtain information about the device, such as the battery level (batterylevel), the battery status (batterystate), the type of device (model, such as the ipod, iphone, etc.), the device system (systemversion) • Through [Uidevice Currentdevice] can get this single-grain object Uidevice object will be a continuous release of some notifications, The following is the name of the notification published by the Uidevice object constant: øuideviceorientationdidchangenotification// Device rotation øuidevicebatterystatedidchangenotification//battery status change øuidevicebatteryleveldidchangenotification// Battery change øuideviceproximitystatedidchangenotification//proximity sensor (e.g., device close to the user's face) • Keyboard Notifications · We often need to do certain things when the keyboard pops up or hides, So you need to listen to the status of the keyboard • When the keyboard state changes, the system will issue some specific notifications Øuikeyboardwillshownotification//keyboard will be displayed øuikeyboarddidshownotification// Keyboard display complete Øuikeyboardwillhidenotification//keyboard is about to hide øuikeyboarddidhidenotification// Keyboard is hidden øuikeyboardwillchangeframenotification//Keyboard position size is about to change øuikeyboarddidchangeframenotification//keyboard position size has changed

• Keyboard Notification • When the system issues keyboard notifications, additional keyboard-related information (dictionaries) is attached, and the dictionary common key is as follows: Øuikeyboardframebeginuserinfokey// Keyboard Frameøuikeyboardframeenduserinfokey//keyboard final frame (after execution of the animation) Øuikeyboardanimationdurationuserinfokey// Keyboard animation time Øuikeyboardanimationcurveuserinfokey//Keyboard animation execution Rhythm (speed) øø notification and proxy selection • Common points ø communication between objects can be accomplished using notifications and proxies

(e.g. a object tells the D object what's going on, a object passes data to the D object)

• Different points o Agent: one-to-many relationships (an object can only tell what happened to another 1 objects) Ø Notification: Many-to-multiple relationships (an object can tell n objects what's going on, 1 objects know what happens to n objects) ·

Notification Center (Nsnotificationcenter)

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.