Notifications in iOS Nsnotification

Source: Internet
Author: User
Tags notification center

Similar to the broadcast receiver sender in Android

1. General and complete notification contains three attributes

1 -(NSString *) name; // name of the notification 2 -(ID)object ; // notifies the publisher (who wants to post a notification) 3 -(nsdictionary *) UserInfo; // Some additional information (informing the publisher of the content of the message sent to the notifying recipient)

2. Initialize a notification (nsnotification) object

1 Object:(ID) anobject; 2 Object:(ID) anobject userInfo: (nsdictionary *) auserinfo; 3 Object:(ID)object userInfo: (nsdictionary *) UserInfo;

3. Publish a notification

1 //Publish a notification notification that sets the name of the notification in the notification object \ Notifies the Publisher \ Extra information, etc.2-(void) Postnotification: (Nsnotification *) notification;3 4 //publish a notification called Aname, AnObject as the publisher of this notification5-(void) Postnotificationnmae: (NSString *) aNameObject:(ID) AnObject;6 7 //publish a notification called Aname, AnObject as the publisher of this notification, auserinfo for additional information8-(void) Postnotificationname: (NSString *) aNameObject:(ID) AnObject UserInfo:(nsdictionary *) Auserinfo;

4. Register a listener for monitoring notifications (Observer)

1 method One:2 Observer: Listener, who wants to receive this notification3 Aselector: After receiving the notification, this method of callback listener, and the notification object as a parameter passed4 AName: The name of the notification, if it is nil, the listener can receive this notification regardless of the name of the notification5 AnObject: Notifies the publisher. If both AnObject and Aname are nil, the listener receives all notifications6-(void) Addobserver: (ID) Observer selector: (SEL) aselector name: (NSString *) aNameObject:(ID) AnObject;7 8 9 Method Two:Ten Name: Names of notifications One obj: The publisher of the notification A BLOCK: When a corresponding notification is received, the block is called back - Queue : The block is destined to execute in which operation queue, if nil, by default in the current Operation queue synchronous execution --(ID) Addobserverforname: (NSString *) nameObject:(ID) obj queue: (nsoperationqueue *) Queue usingblock: (void(^) (Nsnotification *note)) Block

5. Unregister monitoring

1 notification hubs does not hold (retain) listener objects, and objects registered in notification hubs must be unregistered before the object is released. Otherwise, when the corresponding notification appears again, the Notification center will still send the message to the listener, and because the corresponding registration object has been released, So it could cause the app to crash .2 3 How to unregister a listener4 5-(void) Removeobserver: (ID) Observer;6-(void) Removeobserver: (ID) Observer Name: (NSString *) aNameObject:(ID) AnObject;7 8 generally unregister before the listener is destroyed (such as by adding the following code to the listener):9-(void) dealloc{Ten         //[Super Dealloc]; this sentence needs to be called in non-arc One [[Nsnotificationcenter defaultcenter]removeobserver:self]; A}

6.UIDevice Notifications

1 //The Uidevice class provides a singleton 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 ipad, iphone, etc.), device systems (systemversion)2 3 this singleton object can be obtained by [Uidevice Currentdevice]4 5 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:6Uideviceorientationdidchangenotification//Equipment Selection7Uidevicebatterystatedidchangenotification//Battery Status Change8Uidevicebatteryleveldidchangenotification//Battery charge Change9Uideviceproximitystatedidchangenotification//proximity sensors (e.g., the device is close to the user's face)

7. Keyboard notifications

1 //we often need to do certain things when the keyboard pops up or hidden, so we need to listen to the state of the keyboard2 3 //when the keyboard state changes, some specific notifications will be sent to the system .4 5Uikeyboardwillshownotification//The keyboard is about to appear6Uikeyboarddidshownotification//Keyboard Display Complete7Uikeyboardwillhidenotification//The keyboard is about to hide8Uikeyboarddidhidenotification//keyboard hiding complete9Uikeyboardwillchangeframenotification//keyboard position size is about to changeTenUikeyboarddidchangeframenotification//keyboard position size changed One  A  - //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 frame theUikeyboardframeenduserinfokey//Keyboard final frame (after execution of the animation) -Uikeyboardanimationdurationuserinfokey//time of keyboard animation -Uikeyboardanimationcurveuserinfokey//Keyboard animation execution Rhythm (speed)

Agents and Notifications

1 both the agent and the notification send some information to the outside, and then the other objects do something, 2 But the agent can only be a one-to-one relationship 3 and notifications can be many-to-many

Notifications in iOS Nsnotification

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.