ios-on notification mechanism

Source: Internet
Author: User

? 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 particular object is posted)? A complete notification typically contains 3 attributes: 1-(NSString *) name; Name of the notification (ID) of object; Notify the publisher (who is going to post the notice) 3-(nsdictionary *) UserInfo; Some additional information (informing the publisher of the content of the message sent to the notifying recipient)? Initialize a notification (nsnotification) object 1+ (Instancetype) Notificationwithname: (NSString *) aName Object: (ID) anobject;2+ (instancetype) Notificationwithname: (NSString *) AName object: (ID) anobject UserInfo: ( Nsdictionary *) auserinfo;3-(instancetype) Initwithname: (NSString *) Name object: (ID) object userInfo: (Nsdictionary *) UserInfo;

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

?-(void) Postnotification: (nsnotification *) notification; Introduction: Publish a notification notification that sets the name of the notification in the notification object, notifies the publisher, Additional information, etc.-(void) Postnotificationname: (NSString *) AName object: (ID) anobject; Description: Publish a notification named AName, AnObject as the publisher of this notification? void) Postnotificationname: (NSString *) AName object: (ID) anobject userInfo: (nsdictionary *) Auserinfo; Description: Publish a notification called Aname, AnObject as the publisher of this notification, auserinfo for additional information

Registration 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;1.observer: Listener , that is, who wants to receive this notification 2.aSelector: After receiving the notification, callback listener this method, and the notification object as a parameter incoming 3.aName: the name of the notification. If nil, the listener can receive this notification 4.anObject: Notifies the publisher regardless of the name of the notification. 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;1. Name: Notification 2.obj: Notify publisher 3.block: When a corresponding notification is received, this block4.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.To unregister the notification listener:The notification hub does not hold the (retain) 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. Because the listener object has been freed, it could cause the app to crash? The notification hub provides a way to unregister listener 1-(void) Removeobserver: (ID) observer;2-(void) Removeobserver: ( ID) Observer Name: (NSString *) AName object: (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];

}

differences and options for notifications and proxies? common denominator 1. 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)

? 2. Proxy: One-to-many relationships (an object can only tell what happened to another 1 objects) 3. Notification: A Many-to-multiple relationship (an object can tell what happened to n objects, and 1 objects know what happened to n objects)Supplemental: Keyboard Notifications? We often need to do some specific actions when the keyboard pops up or is hidden, so we need to monitor the status of the keyboard?? When the keyboard state changes, the system will issue some specific notifications 1. Uikeyboardwillshownotification//keyboard is about to show 2. Uikeyboarddidshownotification//keyboard display complete 3. Uikeyboardwillhidenotification//keyboard is about to be hidden 4. Uikeyboarddidhidenotification//keyboard hidden complete 5. Uikeyboardwillchangeframenotification//Keyboard position size is about to change 6. Uikeyboarddidchangeframenotification//Keyboard Position size change complete

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.