Objective-C (19th, notification-one of the message sending modes)-iOS development basics, objective-cios
Combine the previous study notes and refer to Objective-C programming full solution (Third edition) To summarize Objective-C knowledge points. Knowledge points keep changing, just for reference,Take the official documents of Apple as standard~
19. Notification-one of the message sending Modes
1. Several Basic Concepts
(1) Notification: In an object-oriented program, it is sometimes necessary to notify the message sending mode of multiple objects of the occurrence time;
(2) Notification center: the intended notification recipient registers the expected notification with the notification center in advance;
(3) Send: if an object sends a message request to the notification center, only the object that has registered the notification can receive the message pushed by the notification center;
(4) Observer: the message sending target. Objects registered in the notification center (multiple objects may exist );
(5) multicast: an object sends messages to a specified object,Notifications are multicast.
2. Notification object: send a message to the notification center. Necessary information will be sent to the notification center after being integrated in an NSNotification instance.
+ (instancetype)notificationWithName:(NSString *)aName object:(id)anObject;+ (instancetype)notificationWithName:(NSString *)aName object:(id)anObject userInfo:(NSDictionary *)aUserInfo;
Variable description:
AName: identifies the short text of the notification, and sends the following message to the NSNotification interface to retrieve the name.
AnObject: the object of the Incidental information sent together with the notification, mostly the object of the notification, alsoCan be nil
UserInfo: Transmission and notification-related information,It can also be nil
3. Notification center nsicationicationcenter
(1) The system has a default notification center, which generally does not need to be created by itself. Class Method acquisition
+ (NSNotificationCenter *)defaultCenter;[NSNotificationCenter defaultCenter];
(2) send notifications
- (void)postNotification:(NSNotification *)notification;- (void)postNotificationName:(NSString *)aName object:(id)anObject;- (void)postNotificationName:(NSString *)aName object:(id)anObject userInfo:(NSDictionary *)aUserInfo;
Specify the notification name, object, and user dictionary to generate the notification, and then send it to the recipient's notification center
(3) observer registration
- (void)addObserver:(id)observer selector:(SEL)aSelector name:(NSString *)aName object:(id)anObject;
Variable description:
Observer: Notification listener
ASelector: After receiving the notification, this method of observer is called.
AName: Notification name. If it is not nil, Only notifications sent from specific objects are accepted;If nil is not set, no sending source is set.;
Object: Notification sender
If you only specify multiple notification names, you can register them in the notification center, or specify the notification name nil to send messages about all notification names, only necessary messages are processed after receiving the message. If both are nil, all notifications about sent messages can be received.(This is the original story in the book)
(4) Delete the registration of the observer
- (void)removeObserver:(id)observer;- (void)removeObserver:(id)observer name:(NSString *)aName object:(id)anObject;[NSNotificationCenter defaultCenter] removeObserver:obj];
(5) about memory management
(1) In the case of reference counting management, when the notification center registers the observer, it does not retain the observer or send the metaobject.Therefore, before releasing these objects, you must delete the relevant settings from the notification center. Otherwise, the pointer pointing to the released object will become a null pointer.; (Remove)
(2) In the garbage collection mechanism, the notifiers and sending source objects use weak references to register in the notification center. Whether registered or not, it may be recycled and released. You do not need to explicitly Delete the observer registration;
I personally understand the following sentence ~
(1) The notification center has a default value. Do not worry about where it is, or create it. You only need to send notifications and observers;
(2) There can be multiple observers, and registration is;
(3) Notification objects can be directly sent without being created;
(4) add and remove the observer's location;
(5) The name is nil and all notifications can be sent.
For code examples in this section, refer to the next
"Notification mode implements two textField values and modal views-iOS development"
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.