iOS design mode nsnotificationcenter Message Center

Source: Internet
Author: User

The Message Center mode is a bit similar to the KVO mode, except that the KVO mode is intended to listen for a change in the corresponding value, and to go to the corresponding action of a method, while the message Center is, broadcast, it is like a broadcast base station, send a message, in all the added listening place can receive this message and make different living the same movements, wider, more powerful

// Add Message Center listener ( add observer, can also be said to add monitoring )     [[NsnotificationcenterDefaultcenter]Addobserver: Selfselector:@selector(mynotification:)name:@ "Test1"Object:Nil];
It listens to test1 the entire value whether someone sends the entire message, the listener is The self of this class, if the listener, it will trigger the mynotification method
     // Span style= "font-family: ' Heiti SC light '; Color:rgb (0,143,0) "> message via message center,
   
// Note: Message center, first listen and then send a message
   
//object: parameters to send
[[nsnotificationcenter defaultcenter]postnotificationname:@ "Test1" Object: Arra];
//This method can be used in any class of the project, so that it can communicate with other places

///For example, there are five pages that create a message center in the first 4 pages to listen for a message @ "change"//We send a @ "change" message on page fifth, via the message center, so that the first four pages can receive this message and then make the appropriate action//must be aware that creating a message center listener must be preceded by a message being sent//The key to solving this problem when using various periodic functions flexibly


Let's take an example.

two Interface interface 1 Interface 2 interface 1 Create a message Center listener message,Interface 2-bed room message center, sending messages with parameters

//Create a message center within an interface 1[[NsnotificationcenterDefaultcenter]Addobserver: Selfselector:@selector(mynotification:)name:@ "Test1"Object:Nil];



implement the Listening method in interface 1 // Supervisor to execute this method when the message is heard -(void) Mynotification: (nsnotification *) n{    //from the received messageN, removeObject, which is the parameter loaded in the message
//nsstring *param = [n Object];
Nsarray *arr = [n Object];
NSLog(@"%@", arr);
}


send an array as an argument in interface 2 Nsarray*arra = [Nsarrayarraywithobjects:@ "a",@ "D",Nil];
//send messages through the message center,
//Note: Message Center, first listen and then send a message
//object:the parameters sent
[[nsnotificationcenter defaultcenter]postnotificationname: @ "test1" object: Arra];

iOS design mode nsnotificationcenter Message Center

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.