Iphone message communication mechanism nsicationcenter Center

Source: Internet
Author: User

 

Nsicationicationcenter is specially set for message communication between different classes in the program, which is extremely convenient to use,

Long story short.

Set the notification, that is, where (which class) the notification is to be received, which is generally done during initialization.

[[Nsicationcenter center defacenter center] addObserver: self selector: @ selector (test :) name: @ "test" object: nil];

I will only describe the above parameters:

AddObserver is the observer, that is, where to receive notifications;

Selector: The method called after receiving the notification;

Name: This is the name of the notification and the unique identifier of the notification. The compiler will find the notification through this.

To send a notification, you must call the method at the observer.

[[Nsicationcenter center defacenter center] postNotificationName: @ "test" object: searchFriendArray];

I will only describe the above parameters:

PostNotificationName: the name of the notification, which is the unique identifier of the notification. The compiler will find the notification through this.

Object: transmitted Parameters

The test method is called by default when a notification is sent.

-(Void) test :( NSNotification *) notification

{

SearchFriendArrary = [notification object]; // get the passed object through this

}

========================================================== ========================

Example ::

========================================================== ========================

@ Implementation chuanzhenAppDelegate

-(Void) onSocket :( AsyncSocket *) sock didReadData :( NSData *) data withTag :( long) tag

{

// Send different messages based on the received messages. Send Login here

[[Nsicationcenter center defacenter center] postNotificationName: @ "Login" object: self];

}

Another BeforeRootViewController. m.

-(Void) viewDidLoad {

[[Nsicationcenter center defacenter center] addObserver: self selector: @ selector (DidLogin) name: @ "Login" object: nil];

}

-(IBAction) LoginClick

{

..............

}

From ruanashi

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.