Notification mechanism for iOS, including notification release, notification listener, notification removal.
1. Notification release
Before you can publish a notification, you create a notification object, a notification object that includes
Notification Publisher: Post notification to notification Center;
Notification Hubs: Receive notifications from notification publishers and forward them to the notification recipients.
Notification recipient: Receives a notification published from the notification hub.
What a notification object should contain:
1. (NSString *) name; // name of the notification
2. (ID) object; // Notify Publishers
3. (nsdictionary *) UserInfo; Some additional information ( informing the publisher of the content of the message sent to the notifying recipient )
Two
1. Create a notification ( Nsnotification) Object method:
+ (instancetype) Notificationwithname: (NSString *) Name object: (ID) object;
+ (instancetype) Notificationwithname: (nsstring *) Name object: (ID) object userInfo: ( nsdictionary *) UserInfo;
-(instancetype) Initwithname: (NSString *) Name object: (ID) object userInfo: (Nsdictionary *) UserInfo;
2. Notification Center (Nsnotificationcenter) provides the appropriate method to help publish the notification
-(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 *) Name object: (ID) object;
Publish a notification called name ,Object is the publisher of this notification
-(void) Postnotificationname: (NSString *) Name object: (ID) object userInfo: (Nsdictionary *) UserInfo;
Publish a name called Name notification, object for the notification Publisher, userinfo for additional information.
3. Notification Center (Nsnotificationcenter) provides a way to register a listener for a listening notification (Observer)
-(void) Addobserver: (ID) Observer selector: (SEL) selector name: (NSString *) Name object: ( ID) Object;
Observer: Listener, who wants to receive this notification
Selector: After receiving the notification, this method of callback listener, and the notification object as a parameter passed
Name: Names of the notifications. If nil, the listener can receive this notification regardless of the name of the notification
Object: Notifies the publisher. 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;
Name: Names of notifications
obj: Notifies the publisher
block: When a corresponding notification is received, the block is called back
queue: Determines which operation queue the block executes in, and if nilis passed, the default is performed synchronously in the current Operation queue.
4. Notification hubs does not hold (retain) listener objects, 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 *) Name object: (ID) object;
Generally unregister before the listener is destroyed (such as by adding the following code to the listener):
-(void) Dealloc {
[superdealloc]; this sentence needs to be called in non- ARC
[[nsnotificationcenterdefaultcenter] removeobserver: Self ];
Like what:
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 // 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 )
Because the keyboard notification is the system automatically, so you can not create a notification, only need to register a listener to listen, when the corresponding notification to execute the method.