The notification center is a subsystem of the foundation framework that broadcasts messages (notifications) to all objects registered as event observers in an application ). (From a programming perspective, it isNSNotificationCenter
Class ). This event can be anything that occurs in the application, such as entering the background status, or the user starts to type in the text bar. The notification tells the observer that the event has occurred or is about to happen, so the observer has the opportunity to respond in the appropriate way. Dissemination of notifications through the notification center is a way to increase the cooperation and cohesion between application objects.
Any object can observe the notification, but to do so, the object must be registered to receive the notification. During registration, it must specify a selector to determine the method called by notification transmission. The method signature must have only one parameter: Notification object. After registration, the observer can also specify the publish object.
(The above is an explanation in the official document)
------------------------------------------ Gorgeous split line ----------------------------------------------------------
The notification center includes two important classes:
(1) nsicationicationcenter:
The principle of implementing nsicationicationcenter is an observer mode. There is only one way to obtain nsnotifcenter, that is, [nsicationicationcenter
Defaultcenter]. You can call the static method defaultcenter to obtain the objects in the notification center. The nsicationicationcenter is a singleton mode, the objects in the notification center will always exist in the lifecycle of an application.
(2)
Nsnotification: This is the carrier of a message. Through this message, the message content can be transmitted to the observer.
(3) A nsicationicationcenter can have many notification messages, nsnotification. For each nsnotification, there can be many observer observers to receive notifications.
Through the above introduction, we can know that the parameter transmission between different classes can also be realized through the notification center.
Note: If you do not want to receive the message again after receiving the message, delete the observer and remove it.
The following describes how to use it (for more information, see the document ).
(1) nsnotification
: Used to create a transmitted message
Creating Notifications+ notificationWithName:object:+ notificationWithName:object:userInfo:Getting Notification Information– name– object– userInfo
(2) nsicationicationcenter: used to send messages
Getting the Notification Center+ defaultCenterManaging Notification Observers– addObserverForName:object:queue:usingBlock:– addObserver:selector:name:object:– removeObserver:– removeObserver:name:object:Posting Notifications– postNotification:– postNotificationName:object:– postNotificationName:object:userInfo:
Demo (the example basically involves all the above methods ):
Two classes are defined: Poster (send message) and observer (accept message)
Poster. h
#import <Foundation/Foundation.h>@interface Poster : NSObject-(void)postMessage;@end
Poster. m
# Import "poster. H "@ implementation poster-(void) postmessage {// 1. the following two statements are equivalent // The difference between the two statements is that the first statement is to send the message content between them. The second statement first creates a message and then sends the message [[nsicationicationcenter defacenter] postnotificationname: @ "posterone" Object: @ "this is posterone! "]; // [[Nsicationcenter center defacenter center] postnotification: [nsnotification notifnotifwithname: @" posterone "Object: @" this is posterone "]; // 2. the following two statements are equivalent // parameter: userinfo --- information about the notification. [[nsicationcenter center defacenter center] postnotificationname: @ "postertwo" Object: @ "this is postertwo" userinfo: [nsdictionary dictionarywithobject: @ "value" forkey: @ "key"]; // [[nsicationicationcenter defacenter center] postnotification: [nsnotification notifnotifwithname: @ "postertwo" Object: @ "this is postertwo" userinfo: [nsdictionary dictionarywithobject: @ "value" forkey: @ "key"];} @ end
Observer. h
#import <Foundation/Foundation.h>@interface Observer : NSObject-(void)observer;@end
Observer. m
# Import "Observer. H "@ implementation observer-(void) Observer {[[nsicationcenter center defacenter center] addobserver: Self selector: @ selector (callback1 :) name: @" posterone "Object: Nil]; [[nsicationcenter center defacenter center] addobserver: Self selector: @ selector (callback2 :) name: @ "postertwo" Object: Nil]; // delete all observers // [[nsnotifcenter center defacenter center] removeobserver: Self]; // Delete the Observer named name // [nsicationicationcenter defacenter center] removeobserver: Self Name: @ "posterone" Object: Nil];}-(void) callback1 :( nsnotification *) Notification {nsstring * namestring = [Notification name]; nsstring * objectstring = [Notification object]; nslog (@ "name = % @, object = % @", namestring, objectstring);}-(void) callback2 :( nsnotification *) notification {nsstring * namestring = [Notification name]; nsstring * objectstring = [Notification object]; nsdictionary * dictionary = [Notification userinfo]; nslog (@ "name = % @, object =%@, userinfo =%@ ", namestring, objectstring, [dictionary objectforkey: @" key "]) ;}@ end
Main. m
# Import <Foundation/Foundation. h> # import "poster. H "# import" Observer. H "int main (INT argc, const char * argv []) {@ autoreleasepool {// note the order here, first observer, then poster Observer * myobserver = [[observer alloc] init]; [myobserver observer]; poster * poster = [[poster alloc] init]; [poster postmessage];} return 0 ;}
Okay, I guess it's all about.
Appendix:
But there is a method
Addobserverforname: object: queue: usingblock:
I still don't know how to use it. I just want to talk about it now.