Listen to notifications from nsicationicationcenter

Source: Internet
Author: User
Tags notification center

1. Problem

You want to listen to the systems and custom notifications that use nsnotification broadcast.

2. Discussion

When you start to listen to notifications, use the object parameters in the addobserver: selector: Name: object: instance method of the notification center to specify the source object (broadcast ).

A brief description of each parameter:

Addobserver: the notification recipient (observer ).

Selector: when the notification observer publishes or receives the notification, the observer calls the selector (method)

Name: name of the notification to be received

Object: select a source object that explicitly publishes a notification. If the parameter is nil, the source object is not considered. If the parameter is already set, only the notification with the specified object name is observed.

3. Example

After the observer is placed in the notification center, you can listen to the notifications sent later.

  

-(Bool) Application :( uiapplication *) Application didfinishlaunchingwitexceptions :( nsdictionary *) launchoptions {// 3.2 listen to notifications sent by nsicationicationcenter/put the observer in the notification center [[nsicationicationcenter defacenter center] addobserver: self selector: @ selector (appendingisfinished :) name: @ "resultofappendingtwostringsnotification" Object: Self]; // 3.1 send notifications through nsicationicationcenter/* use const to save space, avoid unnecessary memory allocation. For example: # define PI 3.14159 // constant macro const doulbe Pi = 3.14159; // pi is not put into ROM at this time ...... double I = PI; // at this time, the PI is allocated with memory and will not be allocated later! Double I = PI; // macro replacement during compilation, allocating Memory double J = PI; // no memory allocation double J = PI; // then macro replacement, allocate memory again! Const defines constants. From the assembly point of view, only the corresponding memory address is given, rather than the number of immediate values as # define. Therefore, the const-defined constants have only one copy during the program running, while the # define-defined constants have several copies in the memory. */Nsstring * firstname = @ "Anthony"; nsstring * lastname = @ "Robbins"; nsstring * fullname = [firstname stringbyappendingstring: lastname]; nsarray * objects = [[nsarray alloc] initwithobjects: firstname, lastname, fullname, nil]; nsarray * keys = [[nsarray alloc] initwithobjects: @ "firststring ", @ "secondstring", @ "resultstring", nil]; nsdictionary * userinfo = [[nsdictionary alloc] initwithobjects: Objects Forkeys: Keys]; // create a notification instance nsnotification * notificationobject = [nsnotification notificationwithname: @ "resultofappendingtwostringsnotification" Object: Self userinfo: userinfo]; // create a notification center to send the notification [[nsnotificationcenter defacenter center] postnotification: notificationobject]; // Note: Sometimes we do not need to set the sender and user information dictionary parameters for each notification. if you do not want to include the sender and user information dictionary, we recommend that you use the postnotificationname: object: instance method in nsicationcenter center. The first parameter is used to input a string to indicate the notification name., The second parameter is passed into nil. Return yes ;}

 

Listen to notifications from nsicationicationcenter

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.