Notification Center Nsnotificationcenter App Summary in iOSFirst, to understand several related classes
1.nsnotification
This class can be understood as a message object with three member variables.
This member variable is a unique identifier for this message object and is used to identify the message object.
@property (readonly, copy) nsstring *name;
This member variable defines an object that can be understood as a message against an object.
@property (readonly, retain) ID object;
This member variable is a dictionary that can be used to pass values.
@property (readonly, copy) nsdictionary *userinfo;
Initialization method for Nsnotification:
-(instancetype) Initwithname: (nsstring *) Name object: (ID) object userInfo: ( Nsdictionary *) UserInfo;
+ (instancetype) Notificationwithname: (nsstring *) AName object: (ID) anobject;
+ (instancetype) Notificationwithname: (nsstring *) AName object: (ID) anobject userInfo: ( nsdictionary *) Auserinfo;
Note: The official documentation is explicitly stated and cannot be initialized with Init
2.Nsnotificationcenter
This class is a notification hub that uses a singleton design and each application has a default notification hub. The acceptance of the send that is used to dispatch notifications.
Add an observer that can specify a method, a name, and an object for it. Executes the method when the notification is accepted.
-(void) Addobserver: (ID) Observer selector: (SEL) aselector name: (nsstring *) AName object: (ID) anobject;
Ways to send notification messages
-(void) Postnotification: (nsnotification *) notification;
-(void) Postnotificationname: (nsstring *) AName object: (ID) anobject;
-(void) Postnotificationname: (nsstring *) AName object: (ID) anobject userInfo: ( Nsdictionary *) Auserinfo;
Ways to remove observers
-(void) Removeobserver: (ID) observer;
-(void) Removeobserver: (ID) Observer name: (nsstring *) AName object: (ID) anobject ;
Some notes:
1. If a notification is sent specifying an object, then the object of the notification set by the observer is the same as that of the object, but the notification is received if the parameter is set to nil.
2, the Observer's SEL function pointer can have a parameter, the parameter is sent by the dead Ossicini object itself, you can take this parameter to the Message object UserInfo, to achieve the value.
second, the use of the notification process
First, we register observers where we need to receive notifications, such as:
Get notification Hubs Singleton Object Nsnotificationcenter * Center = [Nsnotificationcenter Defaultcenter]; Adding the current class object to an observer, name and object set to nil, means receiving all notifications [Center Addobserver:self selector: @selector (notice:) name:@ "123" object: NIL];
After that, send a notification message when we need it
Create a Message object nsnotification * notice = [nsnotification notificationwithname:@ "123" Object:nil userinfo:@{@ "1": @ "123"}]; Send Message [[Nsnotificationcenter Defaultcenter]postnotification:notice];
We can fetch the userinfo content in the callback function, as follows:
-(void) Notice: (ID) sender{NSLog (@ "%@", sender);}
The printing results are as follows:
Where the omission is welcome
Learn to use welcome reprint
Summary of usage of notification hubs (Nsnotificationcenter) in iOS