Notification mechanism of 11.ios

Source: Internet
Author: User
Tags notification center


Notice Center (Nsnotificationcenter)

? 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)



Notice (nsnotification)

? A complete notification typically contains 3 attributes:?-(nsstring*) name; / / name of the notification ?-(ID) object; // Notify Publishers ( who wants to post a notice ) ?-(nsdictionary*) UserInfo; // some extra information. ( notifies the publisher of the content of the message delivered to the notification recipient ) ? Initialize a notification (nsnotification) object? + (instancetype) Notificationwithname: (nsstring*) AName object: (ID) anobject;? + (instancetype) Notificationwithname: (nsstring*) AName object: (ID) Anobjectuserinfo: (nsdictionary*) auserinfo;?-(instancetype) Initwithname: (nsstring*) Name object: (ID) Objectuserinfo: ( nsdictionary *) userInfo;

Publish Notifications

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

?-(void) Postnotification: (nsnotification*) notification;Publish a notification notification to set the name of the notification , the notification publisher , additional information , and so on in the notification object?-(void) Postnotificationname: (nsstring*) AName object: (ID) anobject;publish a notification called Aname, anobject the publisher of this notification?-(void) Postnotificationname: (nsstring*) AName object: (ID) Anobjectuserinfo: ( Nsdictionary*) auserinfo;publish a notification called Aname, AnObject as 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) observerselector: (SEL) Aselectorname: ( NSString*) AName object: (ID) anobject;Observer: Listener, who wants to receive this notificationAselector: After receiving a notification, this method of callback listener, and the notification object as a parameter passed ? 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


?-(ID) addobserverforname: (nsstring*) Name object: (ID) Objqueue: ( Nsoperationqueue*) queue usingblock: (void (^) (nsnotification*note)) block;? Name: Name of the notification? obj: Notifies the publisherBLOCK: When a corresponding notification is received, the block is called backQueue : 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

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. The application may crash because the listener object has been freed? Notification hubs provides a way to unregister listeners?-(void) Removeobserver: (ID) observer;?-(void) Removeobserver: (ID) observername: (nsstring*) AName object: (ID) AnObject;? Typically unregister before the listener is destroyed (such as adding the following code to the listener):

-(void) dealloc{

//[Superdealloc]; Non- ARC this sentence needs to be called in

[[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:? uideviceorientationdidchangenotification // Device Rotation ? uidevicebatterystatedidchangenotification // Battery status Change ? uidevicebatteryleveldidchangenotification // Battery charge Change ? uideviceproximitystatedidchangenotification // near distance 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 / / The keyboard is about to appear ? uikeyboarddidshownotification // Keyboard display Complete ? uikeyboardwillhidenotification / / The keyboard is about to hide ? uikeyboarddidhidenotification // keyboard hiding complete ? uikeyboardwillchangeframenotification / / keyboard position size is about to change ? uikeyboarddidchangeframenotification // keyboard position size changed

? When the system issues keyboard notifications, additional keyboard-related information (dictionaries) are attached, and the dictionary common key is as follows:? Uikeyboardframebeginuserinfokey / / the keyboard just started. Frame ? Uikeyboardframeenduserinfokey / / the final keyboard FRAME ( after the animation has finished executing ) ? Uikeyboardanimationdurationuserinfokey // time of keyboard animation ? Uikeyboardanimationcurveuserinfokey / / Rhythm of keyboard animation execution ( speed )
selection of notifications and proxies

? Common Ground? Communication between objects can be accomplished with notifications and proxies

(for Example a object tells the D Object What happened,the A object passes the data to the d - image )

? Different points? Proxy: One-to-two relationships (1 objects can only tell what happened to another 1 objects )? Notification: A Many-to-many relationship (1 objects can tell what happened to N objects , and1 pairs of icons to know N what happened to an object )




Notification mechanism of 11.ios

Related Article

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.