Notifications in iOS (nsnotification) preface
The notification Center is a single case. Notification iOS
is a design pattern in. Each application has a notification hub NSNotificationCenter
instance that is specifically designed to assist with message communication between different objects.
Any object can be published to the notification center NSNotification
, describing what it is doing, and any object that has registered for the notification will receive this notification when the particular notification is published.
Get notification Hubs Object
Get the notification hubs object in the following way:
123 |
nsnotificationcenter *center = [nsnotificationcenter defaultcenter] ; |
Notification Object Main Properties
A complete notification, the NSNotification
object, has the following three main attributes:
12345 |
@property (readonly, copy) nsstring *name; @property (nullable, readonly, retain) ID object; @property (nullable, readonly, copy) nsdictionary *userinfo; |
The first one is the name of the notification, the second is the notification publisher, and the third is some additional information. For example, the third one can store some data so that the recipient can receive the data.
Notification Object Initialization method
To initialize a notification object, there are several methods:
1234567891011 |
- (instancetype)initwithname:(nsstring *)name Object:(nullable ID)objectuserInfo:(nullable nsdictionary *)userInfo; + (instancetype)notificationwithname:(nsstring *) aName Object:(nullable ID)anobject; + (instancetype)notificationwithname:(nsstring *) AName Object:(nullable ID)anobject userInfo:(nullable nsdictionary *)auserinfo; |
Apple also provides an initialization method, but Apple does not require the use of:
1234 |
/* do not invoke; not a valid initializer to this class * / - (instancetype)init /*ns_unavailable*/; |
Publish Notifications
The announcement of the notification needs to be published through the Notification center NSNotificationCenter
. There are a few three ways to publish notifications:
123456789 |
- (void)postnotification:(nsnotification *)notification ;- (void)postnotificationname:(nsstring *)AName Object:(nullable ID)anobject; - (void)postnotificationname:(nsstring *)AName Object:(nullable ID)anobject userInfo:(nullable nsdictionary *)auserinfo; |
- The first method publishes a notification object directly, and this method uses very few scenarios, with little to see
- The second method is to publish the notification, based on the name of the notification, the object that issued the notification. This approach uses more scenarios when you don't need to pass parameters
- The third method is a parameter to the second method, and the third-party method is primarily able to deliver some additional information while the notification is published
Registration Notice
To be able to receive notifications, you will have to register to notify the notification Center in advance, otherwise you will not receive it. Apple provides two ways to sign up for notifications:
1234567891011 |
- (void)addobserver:(ID)observer selector:(SEL)aselector name:(nullable nsstring *)aNameObject:(nullable ID)anobject; - (ID <NSObject>)addobserverforname:(nullable NSString *)name Object:(nullable ID)obj Queue:(nullable nsoperationqueue *)QUEUE usingblock:(void (^)(nsnotification * Note))block |
The first method is used the most, and the second method is seldom seen in use. The second method is to support block
callbacks, and the queue
arguments are in which queue.
Cancel Registration Notice
Notifications are registered, and you need to remove them if you don't need them. The notification hub does not retain the listener object, and the object that is 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. Because the corresponding listener object has been freed, it can cause the app to crash, which is a common type of crash.
Apple offers two ways to remove a registration:
1234567 |
- (void)removeobserver:(ID)observer; - (void)removeobserver:(ID)observer name:(nullable nsstring *)aName Object:(nullable ID)anobject; |
Normally, we will dealloc
cancel the registration notification before the controller:
12345 |
- (void)dealloc { [[ Nsnotificationcenter defaultcenter] removeobserver: Self]; } |
If we just want to cancel the registration of a notification, not all of it, then you can use the second method to pass the notification name, and you will only unregister the notification.
Keyboard notifications
For keyboards, we often need to register and remove registration notifications:
12345678 |
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 |
Notifications in iOS (nsnotification)