Use of the IOS Nsnotification Center notification Hub

Source: Internet
Author: User
Tags notification center

Notification hubs, which is a message broadcast mechanism inside an iOS program. Through it. The ability to implement 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 center receives the message, it sends the message to the Subscriber based on the settings, where subscribers can have more than one.

Notification hubs, similar to proxy mode, enable multiple inter-object communication, notification hubs can send one notification to multiple listeners, while proxy mode can only join one agent per object. But regardless of that pattern, it is a low-coupling design that implements communication between objects.

Steps to use notification hubs

1. Note that the Observer is interested in an event (named after a string) and sets the selector or block to run 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 runs the corresponding selector or block

4. Remove Notifications

Notification Center case

Use the navigation to add three interfaces, add a set of buttons to the third interface, when you click on the button. Sets the current page background color to the button color, and sends a notification that sets the previous two interface background color to the selected color as well. What the program framework and interface see

  (1) Register notice, in the interface three Viewdidload method, for interface one and interface two register notice, when sending notification, will go to interface one and interface two 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 on the Color button, change the background color, but also to interface one and interface two 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. Removing a notification generally overrides the Dealloc method of the parent class. But under the ARC mode. The Dealloc method of the parent class cannot be called in the Dealloc method

  Interface three of the code:

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

A small partner who wants to learn a lot about other content. You can click to view the source code and perform the test yourself.

Inquiries or technical exchanges. Please add the official QQ Group: (452379712)

Jerry Education
Source:http://blog.csdn.net/jerehedu/
This article belongs to Yantai Jerry Education Technology Co., Ltd. and CSDN co-owned. Welcome reprint. However, without the permission of the author, the statement must be retained and the original text connected in the obvious position on the article page, otherwise the right to be held liable.

Use of the IOS Nsnotification Center notification Hub

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.