First, 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:
?
1234 |
//获取通知中心单例对象 NSNotificationCenter * center = [NSNotificationCenter defaultCenter]; //添加当前类对象为一个观察者,name和object设置为nil,表示接收一切通知 [center addObserver:self selector:@selector(notice:) name:@ "123" object:nil]; |
After that, send a notification message when we need it
?
1234 |
//创建一个消息对象 NSNotification * notice = [NSNotification notificationWithName:@ "123" object:nil userInfo:@{@ "1" :@ "123" }]; //发送消息 [[NSNotificationCenter defaultCenter]postNotification:notice]; |
We can fetch the userinfo content in the callback function, as follows:
?
123 |
-( void )notice:(id)sender{ NSLog(@ "%@" ,sender); } |
The printing results are as follows:
Notification Center Nsnotificationcenter App Summary in iOS