1. Notification 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)
2. Notice (nsnotification)
A complete notification generally contains 3 attributes:-(NSString*) name;// name of the notification- (ID) object;// Notify Publishers(who is going to post the notice)- (nsdictionary*) UserInfo;// some extra information.(notifies the publisher of the content of the message delivered to the notification recipient)"Initializes 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;
3. Release Notifications
Notification hubs (Nsnotificationcenter) provides the appropriate method to help publish notifications
》- (void) Postnotification: (nsnotification*) notification; Publishes a notification notification that can be set in the notification objectname of the notification、Notify Publishers、Additional InformationAnd so on-(void) Postnotificationname: (NSString*) AName object: (ID) AnObject; Publish a notification called Aname, AnObject is 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
3. Register Notification listeners
Notification Center (Nsnotificationcenter) provides a way to register a listener for a listening notification (Observer)
- (void) Addobserver: (ID) Observerselector: (SEL) Aselectorname: (NSString*) AName object: (IDAnobject;observer: Listener, that is, who wants to receive this notification Aselector: After receiving the notification, callback listener this method,and passing the notification object as a parameterAName: 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: Names of notificationsobj: 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 .
4. Unregister notification listener Notification Center does not retain (retain) listener objects, objects registered in notification hubs,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 corresponding listener object has been freed, it may cause the app to crash the notification hub provides a way to unregister the listener-(void) Removeobserver: (ID) Observer; - (void) Removeobserver: (ID) Observername: (NSString*) AName object: (IDAnObject; "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
[[nsnotificationcenter defaultcenter] removeobserver:Self];
}
5.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.), device System (systemversion) by [Uidevice Currentdevice] can get this single-grain object The Uidevice object will publish a number of notifications without interruption, following the name constants of the notifications published by the Uidevice object:uideviceorientationdidchangenotification//Device Rotationuidevicebatterystatedidchangenotification//Battery Status Changeuidevicebatteryleveldidchangenotification//Battery charge Changeuideviceproximitystatedidchangenotification//near distance sensor(For example, the device is close to the user's face)
6. 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 keyboardThe system will issue specific notifications when the keyboard status changes uikeyboardwillshownotification// keyboard is about to be displayed uikeyboarddidshownotification// keyboard display complete uikeyboardwillhidenotification// keyboard is about to be hidden uikeyboarddidhidenotification// keyboard hidden complete 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 frame uikeyboardframeenduserinfokey// keyboard final frame ( after the animation has finished executing ) uikeyboardanimationdurationuserinfokey// keyboard animation time uikeyboardanimationcurveuserinfokey// keyboard animation execution Rhythm ( speed )
7. notification and selection of agents "Common Groundcommunication between objects can be accomplished with notifications and proxies
(For example a object tells the D Object What's going on,a object passes the data to D To the elephant )
"Different pointsAgent: One-to-two relationships (1 objects can only tell the other 1 Objects What's going On)notification: A Many-to-many relationship (1 objects can tell what happened to N Objects ,1 pairs of icons To know what's going on with N objects )
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
26th article: Notification Center Nsnotificationcenter