Development Notes-Notifications

Source: Internet
Author: User
Tags notification center

1. Notification: Publish/Listen/remove
    • Each application has a notification hubs (Nsnotificationcenter) instance that is specifically designed to assist 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 specific object is posted)
A complete notification typically consists of 3 attributes:
    • -(nsstring*) name of the name;//notification
    • -(ID) object;//notify the publisher (who is going to post the notice)
    • -(nsdictionary*) userinfo;//Some additional information (informing the publisher of the content of the information communicated to the notifying recipient)
Notification (nsnotification) Initializes a notification (nsnotification) object
    • Object:(IDObject:(ID) anobject userInfo: (nsdictionary*   Object:(ID)object userInfo: (nsdictionary*) UserInfo;

Publish Notifications

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

    • -(void) Postnotification: (nsnotification*-(voidObject:(ID  -(voidObject:(ID) anobject userInfo: (nsdictionary*) auserinfo; publish a notification called Aname, AnObject is the publisher of this notification, auserinfo for additional information

Registering a 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, 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;
    • Name: Names of 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.
Unregister Notification Listener
    • Notification hubs does not hold (retain) listener objects, and objects 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 may crash because the listener object has been freed

    • Notification hubs provides the appropriate method to unregister listeners
    • -(void) Removeobserver: (ID) Observer;id object no longer listens on any object -(void) Removeobserver: (  IDObject:(ID) anobject;
    • Generally unregister before the listener is destroyed (such as by adding the following code to the listener):
-(void) dealloc {  //[Super Dealloc];  This sentence needs to be called in non-arc     [[Nsnotificationcenterdefaultcenter] removeobserver:self];}

Uidevice Notice
    • The Uidevice class provides a single-grain 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 system of the device (systemversion)
    • This single-grain object can be obtained by [Uidevicecurrentdevice]
    • The Uidevice object publishes a number of notifications without interruption, and the following is the name constant of the notification published by the Uidevice object:
    • // Device Rotation // Battery Status Change Uidevicebatteryleveldidchangenotification//  battery Change //  proximity sensor (e.g. the device is close to the user's face)

    • Keyboard notifications
    • We often need to do certain things when the keyboard pops up or hidden, so we need to listen to the state of the keyboard
When the keyboard state changes, some specific notifications will be sent to the system.
    • Uikeyboardwillshownotification//  keyboard is about to show 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 change complete

When the system issues a keyboard notification, it comes with additional keyboard-related information (a dictionary), and the dictionary common key is as follows:
    • Uikeyboardframebeginuserinfokey//  keyboard just started frameuikeyboardframeenduserinfokey//  Keyboard final frame (after completion of animation)Uikeyboardanimationdurationuserinfokey//  keyboard animation time Uikeyboardanimationcurveuserinfokey//  keyboard animation execution Rhythm (speed)

Selection of notifications and proxies
    • Common
    • Communication between objects can be accomplished with 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
    • Proxy: 1 objects can only tell another 1 objects what's going on
    • Notification: 1 objects can tell what happened to n objects, and 1 objects know what happened to n objects.
click-to-click Method Publish notification, controller supervisor hear notification, call to listen on that methodSequence: Listen first so that the program has started loading the Viewdidload method to listen Cell:Monitoring notifications
1- (void) Viewdidload {2 [Super Viewdidload];3     //Get the Notification center .4Nsnotificationcenter *center =[Nsnotificationcenter Defaultcenter];5      //Add Listener6[Center addobserver:self selector: @selector (plusclick:) Name:@"plusclicknotification" Object: nil];7 }8                                                                            Object: nil]-----Whoever publishes it can9 Observer: ViewerTen //Unregister notification Listener One- (void) Dealloc A { - [[Nsnotificationcenter Defaultcenter] removeobserver:self]; -}

-------------------------------------------------------------------------------------------------------- Controller:#pragma mark-monitoring notification-How to listen for triggering calls
-(void) Plusclick: (nsnotification *) note{    //  Remove cell (notification publisher)    Xmgwinecell *cell = Note. Object ;     // Calculate Total Price    int totalprice = self.totalPriceLabel.text.intValue +;     // Set Total Price    Self.totalPriceLabel.text = [NSString stringWithFormat:@ "%d", Totalprice];}

* Plus click
-(ibaction) Plusclick {//Modifying a; //Modify Quantity LabelSelf.countLabel.text = [NSString stringWithFormat:@"%d",]; //minus can clickself.minusButton.enabled =YES; //Publish Notifications[[Nsnotificationcenter Defaultcenter] Postnotificationname:@"plusclicknotification" Object: self];}default: Default


Development Notes-Notifications

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: 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.