Notifications for iOS

Source: Internet
Author: User

First of all, what is the notification, the function of notification and agent is the same, is the value of the transfer, the method, but I personally feel that the notification is more convenient than the agent, and more powerful, if you understand the agent, then the notice is very easy to understand, I think the notification is easier than the agent writing, And more powerful. The agent is a pair of one-pass value, but the notification can be a pair of more or more to many, well, not much to say to everyone specific talk about:

First look at the concept of notification hubs:

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)

This is a simple notification:

A complete notification typically consists of 3 attributes:

-(NSString *) name;//name of the notification- (ID)Object;//notifies the publisher (who wants to post a notification)-(Nsdictionary *) UserInfo;//Some additional information (informing the publisher of the content of the message sent to the notifying recipient)Initializes a notification (nsnotification) object+ (Instancetype) Notificationwithname: (NSString *) aNameObject:(ID) AnObject;+ (Instancetype) Notificationwithname: (NSString *) aNameObject:(ID) AnObject UserInfo: (Nsdictionary *) Auserinfo;-(Instancetype) Initwithname: (NSString *) nameObject:(ID)ObjectUserInfo: (nsdictionary *) UserInfo;

To publish a notification:

//notification hubs (Nsnotificationcenter) provides the appropriate method to help publish notifications- (void) Postnotification: (Nsnotification *) notification;//Publish a notification notification that sets the name of the notification, the notification Publisher, additional information, and so on in the notification object- (void) Postnotificationname: (NSString *) aNameObject:(ID) AnObject;//publish a notification called Aname, AnObject as the publisher of this notification- (void) Postnotificationname: (NSString *) aNameObject:(ID) AnObject UserInfo: (Nsdictionary *) Auserinfo;//publish a notification called Aname, AnObject as the publisher of this notification, auserinfo for additional information

Register the Listener:

// notification hubs (Nsnotificationcenter) provides a way to register a listener for a listening notification (Observer)-(void) Addobserver: (ID  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 //AnObject, regardless of the name of the notification: notifies the publisher. If both AnObject and Aname are nil, the listener receives all notifications

To unregister the monitor notifier:

//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;- (void) Removeobserver: (ID) Observer Name: (NSString *) aNameObject:(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[[Nsnotificationcenter Defaultcenter] removeobserver:self];}

Uidevice Notice

 // uidevice class provides a single-grain object that represents the device, It provides information about devices such as battery level (batterylevel), battery status (batterystate), type of 
device (model, such as ipod, iphone, etc.), device system (systemversion)
// by [Uidevice Currentdevice] You can get this single-grain object // uidevice objects will publish some notifications without interruption, The following is the name constant for the notification published by the Uidevice object: uideviceorientationdidchangenotification // device rotation Uidevicebatterystatedidchangenotification // Battery status change uidevicebatteryleveldidchangenotification // battery charge change uideviceproximitystatedidchangenotification // proximity sensors (e.g. devices close to the user's face)

Here are a few commonly used notifications about keyboards:

//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//The keyboard is about to appearUikeyboarddidshownotification//Keyboard Display CompleteUikeyboardwillhidenotification//The keyboard is about to hideUikeyboarddidhidenotification//keyboard hiding completeUikeyboardwillchangeframenotification//keyboard position size is about to changeUikeyboarddidchangeframenotification//keyboard position size changed//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//the keyboard just started frameUikeyboardframeenduserinfokey//Keyboard final frame (after execution of the animation)Uikeyboardanimationdurationuserinfokey//time of keyboard animationUikeyboardanimationcurveuserinfokey//Keyboard animation execution Rhythm (speed)

The respective characteristics of the notification and agent:

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:
Agent: One-to-two relationships (an object can only tell what happened to another 1 objects)
Notification: A Many-to-many relationship (an object can tell what happened to n objects, and 1 objects know what happened to n objects)

Also attached is an example of a keyboard notification:

// Register a notification
Object : nil];
-(void) dealloc {    //[Super Dealloc];  This sentence needs to be called in non-arc     [[Nsnotificationcenter Defaultcenter] removeobserver:self];}    
//called when the keyboard changes the frame- (void) Keyboardwillchangeframe: (Nsnotification *) note{//Animation TimeCGFloat Duration =[Note.userinfo[uikeyboardanimationdurationuserinfokey] doublevalue];//get the last frame of the keyboardCGRect keyboardfrm =[Note.userinfo[uikeyboardframeenduserinfokey] cgrectvalue];//calculate the distance that the controller view needs to be pannedCGFloat viewtransformy = KEYBOARDFRM.ORIGIN.Y-Self.view.frame.size.height;//Performing animations[UIView animatewithduration:duration animations:^{self.view.transform= Cgaffinetransformmaketranslation (0, Viewtransformy); }]; }

Notifications for iOS

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.