first, to understand several related classes1, 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. (Add observer on desired page)
-(void) Addobserver: (ID) Observer selector: (SEL) aselector name: (NSString *) AName object: (ID) anobject;
method of sending notification messages (notifications are sent when certain changes occur to the viewer)
-(void) Postnotification: (nsnotification *) notification;
-(void) Postnotificationname: (NSString *) AName object: (ID) anobject;
-(void) Postnotificationname: (NSString *) AName object: (ID) anobject userInfo: (nsdictionary *) Auserinfo;
Method of removing the observer (placed in the Dealloc method)
-(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 to observe the object itself, through this parameter can be taken 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:
Iii. example of using the system's own notification
1. Adding observers
[[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (textChanged:) Name: Uitextviewtextdidchangenotification Object:self.textView];
2, because the use of Uitextview comes with the notification uitextviewtextdidchangenotification, so the notification will be Uitextview edit the end of the time automatically sent, do not need to post
3. Call the corresponding textchanged after receiving the notification: method
-(void) textChanged: (nsnotification*) Noti {
What you want to do when you receive a notification
}
4. Remove Notifications
-(void) Dealloc {
[[Nsnotificationcenter Defaultcenter] removeobserver:self name:uitextviewtextdidchangenotification object: Self.addresstextview];
}
Notification hubs Nsnotificationcenter (custom or system-brought) in iOS application summary