Objective-c (19, one of the notification-message delivery modes)--ios Development Basics

Source: Internet
Author: User
Tags notification center

Combining the previous study notes and referring to the full solution of Objective-c Programming (third edition), this paper summarizes the objective-c knowledge points. Knowledge points have been changing, just as a reference to the official Apple document ~

19. Notification-One of the message delivery modes

1. Several basic concepts
(1) Notification: In the object-oriented program, it is sometimes necessary to notify the occurrence of the time to multiple objects of the message delivery mode;
(2) Notification Center: the object that expects to be notified in advance to the notification center to register the expected to obtain notice;
(3) Send: An object sent a message to the notification center to send a request, only registered the amount of the notice object, you can get notification center push message;
(4) Observer: Message sending target, object registered in Notification Center (can have multiple);
(5) Multicast: An object sends a message to a specific number of objects, and a notification is a multicast

2. Notification object: Send a message to the notification center, and the necessary information will be sent to the notification center after it is set in the Nsnotification class instance

+ (instancetype)notificationWithName:(NSString *)aNameobject:(id)anObject;+ (instancetype)notificationWithName:(NSString *)aName                              object:(id)anObject                            userInfo:(NSDictionary *)aUserInfo;

Variable Description:
AName: To identify the short text of the notice, send the following message to the Nsnotification interface to remove the name
AnObject: The object with the information sent with the notification, more than the object to send the notification, also can be nil
UserInfo: A variety of information related to the delivery and notification, also can be nil

3. Notification Center Nsnotificationcenter
(1) The system has a default notification center, generally do not create their own. Class method gets

+ (NSNotificationCenter *)defaultCenter;[NSNotificationCenter defaultCenter];

(2) Notification sent

- (void)postNotification:(NSNotification *)notification;- (void)postNotificationName:(NSString *)aName object:(id)anObject;- (void)postNotificationName:(NSString *)aName                      object:(id)anObject                    userInfo:(NSDictionary *)aUserInfo;

Specify notification names, objects, and user dictionaries to generate notifications and send them to the recipient's notification hub

(3) Viewer Registration

- (void)addObserver:(id)observer           selector:(SEL)aSelector               name:(NSString *)aName             object:(id)anObject;

Variable Description:
Observer: Notification of the Listener
Aselector: This method of observer is called when a notification is received.
ANAME: Notification name, when not nil, only accept notifications sent by a particular object, if no nil indicates no set of sending sources ;
Object: Sender of Notification

when you specify only notifications for a specific number of notification names, you can register each notification name in the notification hub, or specify that the notification name is nil so that it sends a message about all the notification names, and that only the necessary messages are processed when they are received, and both nil can receive all notifications about sending messages (This is the book's exact words)

(4) Deleting the viewer's registration

- (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 count management, the Notification Center does not retain observers and send meta objects when registering the observer. So before releasing these objects, you really need to remove the settings from the notification hub. Otherwise, a pointer to the disposed object becomes a null pointer ; (that is, remove)
(2) in the garbage collection mechanism, the notifier and the sending source object are registered in the notification hub using weak references. Whether or not it is registered, it may be reclaimed. No explicit deletion of the observer's registration is required;

I personally understand briefly, is the following sentence of the vernacular ~

(1) Notification Center has a default, do not tangle it in the end where, or to create, as long as the notification and observers can be sent;
(2) A plurality of observers may be registered;
(3) Inform the object can not be created, direct notification can be sent;
(4) The local add,remove of the Observer;
(5) Name Nil, can accept all notifications sent

This part of the code example, please refer to the next article
"Notification mode realizes two TextField and modal view--ios development"

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Objective-c (19, one of the notification-message delivery modes)--ios Development Basics

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.