Use of the IOS Nsnotification Center notification Hub

Source: Internet
Author: User
Tags notification center

Notification hub, which is a message broadcast mechanism inside an iOS program that enables communication between objects without referential relationships. Notification Center He is based on the observer pattern, which can only communicate within the program, not across application processes. When the notification hub receives the message, it sends the message to the Subscriber based on the settings, where subscribers can have more than one.

Notification hubs, like proxy mode, can implement multiple inter-object communication, and notification hubs can send one notification to multiple listeners, while proxy mode can only add one agent per object. However, the model is a low-coupling design that implements communication between objects.

Steps to use notification hubs

1. The registered observer is interested in an event (named after a string) and sets the selector or block to be executed when the event is triggered

2. Nsnotificationcenter an event (named in a string) at a certain time

3. When an observer receives an event of interest, it executes the corresponding selector or block

4. Remove Notifications

Notification Center case

Using the navigation to add three interface, add a group of buttons on the third interface, when the button is clicked, set the current page background color for the button color, and send a notification, the front two interface background color is also set to select the color, program framework and interface

  (1) Registration notice, in the interface three Viewdidload method, for interface one and interface two registration notice, when sending notification, will go to interface one and interface two to call the corresponding method

  Interface three of the code:

-(void) viewdidload {    [super viewdidload];        Secondviewcontroller *SECONDVC = [Self.navigationController.viewControllers objectatindex:1];    Viewcontroller *FIRSTVC = [Self.navigationController.viewControllers firstobject];    Registration Notice    [[nsnotificationcenter defaultcenter] ADDOBSERVER:SECONDVC selector: @selector (changebgcolor:) Name: Knotificationname Object:nil];    [[Nsnotificationcenter Defaultcenter] ADDOBSERVER:FIRSTVC selector: @selector (changebgcolor:) Name: Knotificationname Object:nil];}

Ps:knotificationname is a string defined by a macro definition

  (2) In the interface three click the Color button, change the background color, and give the interface one and interface two to send notifications

  Interface three of the code:

-(Ibaction) Choosebgcolor: (UIButton *) Sender {     Nsarray *colorarray = @[[uicolor Redcolor],[uicolor bluecolor],[ Uicolor greencolor],[uicolor purplecolor],[uicolor whitecolor],[uicolor blackcolor],[uicolor orangeColor],[UIColor Yellowcolor],[uicolor Browncolor]];        Self.view.backgroundColor = [Colorarray objectatindex:sender.tag-1];        Raise Notification    [[nsnotificationcenter Defaultcenter] Postnotificationname:knotificationname Object:[colorarray Objectatindex:sender.tag-1]];  }

(3) Processing notifications in interface one and interface two, changing the background color of the interface

  Interface one and interface two in the code:

-(void) Changebgcolor: (nsnotification *) notification{    [Self.view setBackgroundColor:notification.object];}


(4) In the interface three removal notice, remove the notification is generally overridden in the parent class's Dealloc method, but in the arc mode, the parent class's Dealloc method cannot be called in the Dealloc method

  Interface three of the code:

-(void) dealloc{     [[Nsnotificationcenter Defaultcenter] removeobserver:knotificationname];}

To learn more about the small partners, you can click to view the source code , run the test yourself.

Inquiries or technical exchanges, please join the official QQ Group: (452379712)

Jerry Education
Source:http://blog.csdn.net/jerehedu/
This article is the copyright of Yantai Jerry Education Technology Co., Ltd. and CSDN Common, welcome reprint, but without the author's consent must retain this paragraph statement, and in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Use of the IOS Nsnotification Center notification Hub

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.