Nsicationcenter Center)

Source: Internet
Author: User

The message mechanism NSNotificationCenter has been frequently used, but does not have a good understanding of its principles. Today, it takes some time to repeat the principles of the message mechanism to the end. IOS provides a "synchronous" Message notification mechanism. An observer only needs to register with the message center to receive messages sent from other objects. The message sender and the message recipient can have no idea about each other, completely decoupled. This type of message notification mechanism can be applied to any time and any object, and there can be multiple observers. Therefore, messages are broadcast, but note that after the observer registers with the message center, you need to log out of the message center when you do not need to receive messages. This message broadcast mechanism is a typical "Observer" mode. This requirement is also easy to implement. each running application has a member variable of nsicationicationcenter. Its function is similar to the public column. object Registration follows a specific notification (if someone finds a puppy, Let me know ). we call these registration objects observer. other Objects will send notifications to the center (I picked up a puppy ). center forwards the notifications to all objects that are registered with the notification. we call these notification objects a poster message mechanism, which is often used to request data or submit data to the server. After successful interaction with the server, we need to process the data returned by the server, or send a response message. This article prohibits reprinting on any website and severely condemn the worms.
This article was first published in, blog garden. Please search: blog garden-Find yourself and view the original article
The first address of this article is IOS message center (nsicationicationcenter)-http://www.cnblogs.com/xunziji/p/3257447.htmluse the messaging mechanism:

1. The observer registers the Message notification.

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getUserProfileSuccess:) name: :nil];

NotificationObserver observer: self
Icationicationselector: getUserProfileSuccess
Icationicationname: Notification_GetUserProfileSuccess
Icationicationsender message sender: indicates the sender who receives the notification. If the fourth parameter is nil, it receives the notification from all senders.

 

2. Send a message notification

// UserProfile Is A Model
// @ Interface UserProfile: NSObject

[[NSNotificationCenter defaultCenter] postNotificationName: :userProfile userInfo:nil];

Icationicationsender message sender: userProfile

 

This article prohibits reprinting on any website and severely condemn the worms.
This article was first published in, blog garden. Please search: blog garden-Find yourself and view the original article
This article first address: IOS message mechanism (nsicationcenter Center)-http://www.cnblogs.com/xunziji/p/3257447.html

3. The observer processes the message.

- () getUserProfileSuccess: (NSNotification*= [aNotification =======

 

Message received by NSNotification, mainly including:
Name: Message Name icationication_getuserprofilesuccess
Object: message sender userProfile
UserInfo: message transmission data

 

This article prohibits reprinting on any website and severely condemn the worms.
This article was first published in, blog garden. Please search: blog garden-Find yourself and view the original article
This article first address: IOS message mechanism (nsicationcenter Center)-http://www.cnblogs.com/xunziji/p/3257447.html

4. The observer logs out and removes the message observer.

Although after using ARC in IOS, removing NSNotification Observer is not displayed and there is no error, this is a bad habit and is not conducive to performance and memory.

There are two methods to deregister an observer:

A. Optimal Method, in UIViewController. m:

-(

If you see the method you don't need to call [super dealloc]; here, only the method without super dealloc needed.

 

B. Single removal:

[[NSNotificationCenter defaultCenter] removeObserver:self name: :nil];

This article prohibits reprinting on any website and severely condemn the worms.
This article was first published in, blog garden. Please search: blog garden-Find yourself and view the original article
This article first address: IOS message mechanism (nsicationcenter Center)-http://www.cnblogs.com/xunziji/p/3257447.html

Related Article

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.