IOS notification center
In general, the message transmission mechanism in iOS development is divided into three categories:
Local notification
Push notification
Notification center
There is no direct relationship between the three. The notification center registers all the objects of an observer to the application to broadcast messages. Through the notification center, message communication between different objects can be well coordinated.
Note:
Each application has only one nsicationicationcenter instance.
Both the proxy and the notification center coordinate message communication between objects. The function of the notification center is similar to that of the proxy. What are the differences between the proxy and the notification center?
The one-to-one relationship corresponding to the proxy, and the notification is a one-to-many relationship. Therefore, when a change is required, more
We recommend that you select notifications for the changed functions.
For example:
Change skin effect on QQ music App
The reading software changes the font size.
Use the two properly during actual development to prevent minor usage
The successful implementation of a notification must have three elements
Notification publisher
Notification center
Notification recipient
A complete notification contains three attributes.
Name: Notification name
Object: Notification publisher
UserInfo: the information that the notification recipient sends to the notification recipient.
Notification recipient registration method to notification center
-(Void) addObserver :( id) observer selector :( SEL) aSelector name :( nullable NSString *) aName object :( nullable id) anObject; observer: Listener, that is, who wants to receive this notification aSelector: after receiving the notification, callback the listener's method and pass the notification object as a parameter to the aName: Notification name. If it is nil, the listener will receive the anObject notification, regardless of the notification name. If both anObject and aName are nil, the listener receives all notifications.
Three methods for publishing notifications in the notification center
? - (void)postNotification:(NSNotification *)notification;? - (void)postNotificationName:(NSString *)aName object: (id)anObject;? - (void)postNotificationName:(NSString *)aName object:(id)anObjectuserInfo:(NSDictionary *)aUserInfo;
Dealloc method under notification
After we register in the notification center, the notification center is very sensitive. When the condition is triggered, messages will be sent continuously to the registrant, but when our registrant has been released, this will cause a wild pointer and cause program crash. Therefore, when an object is released, we should release the notification registered with the notification center.
- (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; }
In addition, there are two methods to cancel registration:
-(Void) removeObserver :( id) observer;-(void) removeObserver :( id) observer name :( nullable NSString *) aName object :( nullable id) anObject; // The second parameter with one name added can be specific to a notification in this object.
Common application of the notification center
1. device Rotation
UIDeviceOrientationDidChangeNotification
2. keyboard operations
UIKeyboardWillShowNotification // The UIKeyboardDidShowNotification is displayed on the keyboard. // The UIKeyboardWillHideNotification is displayed on the keyboard. // the keyboard is hidden soon. The keyboard position is about to change. // The keyboard position is changed. size changed
Reminder
The normal execution order of the notification is: the recipient registers the notification in the notification center --> the notification recipient publishes the notification in the notification center ---> releases the registration notification from the notification center. An error occurs when an order error occurs.