Nsnotification for iOS development

Source: Internet
Author: User

What is notification?

In my personal understanding, a person is registered as a member in a department, that is, the registration mentioned below is called a listener. Let the listener listen to an event for you. When the listener listens to an event, send a notification: postnotification is sent to nsicationicationcenter, and then the listener executes the method in the selector. The following describes the process:

Register the listener first:

[[Nsicationcenter center defacenter center] addobserver: Self selector: @ selector (gochange :) name: @ "changecolor" Object: Nil];

Last object: Nil indicates that the message is sent to all listeners. if the message is written to an object, only the notification is sent to the object.

Here, my consideration is: nsicationicationcenter is like a container for storing messages. We can either store information in it or extract information from it.

Gochange: The implementation of the method is as follows:

-(Void) gochange :( nsnotification *) Notification

{

// Get the notification content.

Nsdictionary * DIC = [Notification userinfo];

Nsstring * colorstring = [DIC objectforkey: @ "color"];

Uicolor * color = nil;

If ([colorstring isequaltostring: @ "red"]) {

Color = [uicolor redcolor];

} Else if ([colorstring isequaltostring: @ "blue"]) {

Color = [uicolor bluecolor];

}

Self. View. backgroundcolor = color;

}


The implemented function is: I have two buttons. When the red button is clicked, the background color turns red, and when the blue button is clicked, the background color turns blue. The two buttons are set as follows:

Redbutton:

-(Void) setredcolor

{

Nsdictionary * DIC = [nsdictionary dictionarywithobject: @ "red" forkey: @ "color"];

[[Nsicationcenter center defacenter center] postnotificationname: @ "changecolor" Object: Self userinfo: DIC];

}

Bluebutton:

-(Void) setbluecolor

{

Nsdictionary * DIC = [nsdictionary dictionarywithobject: @ "blue" forkey: @ "color"];

[[Nsicationcenter center defacenter center] postnotificationname: @ "changecolor" Object: Self userinfo: DIC];

}


Then there is the following sentence:

[[Nsicationcenter center defacenter center] postnotificationname: @ "changecolor" Object: Self userinfo: DIC];


Postnotification is used to save the parameter and send the created notification to nsnotificationcenter;


My understanding is: When you click the button, check icationicationname. If this name is the same as postnotificationname, the notification is sent successfully and the method of the listener selector is executed. The purpose of notifying you personally is achieved.


If you have any questions, you are welcome to raise them. This is also a technical blog post I just started to write. There will certainly be many vulnerabilities. You are welcome to criticize and correct them. I believe that the more I will do, the better I will.

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.