Use of the IOS Nsnotification Center notification Hub
Source: Internet
Author: User
<span id="Label3"></p><p align="left"><p align="left"><span style="font-family: Microsoft YaHei; font-size: 14px;">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.</span></p></p><p align="left"><p align="left"><span style="font-family: Microsoft YaHei; font-size: 14px;">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.</span></p></p>Steps to use notification hubs <blockquote> <blockquote> <p align="left"><span style="font-family: Microsoft YaHei; font-size: 14px;">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</span></p> <p align="left"><span style="font-family: Microsoft YaHei; font-size: 14px;">2.</span> <span style="font-family: Microsoft YaHei; font-size: 14px;">Nsnotificationcenter an event (named in a string) at a certain time</span></p> <p><span style="font-family: Microsoft YaHei; font-size: 14px;">3. When an observer receives an event of interest, it executes the corresponding selector or block</span></p> <p><span style="font-family: Microsoft YaHei; font-size: 14px;">4.</span> <span style="font-family: Microsoft YaHei; font-size: 14px;">Remove Notifications</span></p> </blockquote> </blockquote>Notification Center case<p><p><span style="font-family: Microsoft YaHei; font-size: 14px;">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</span></p></p><p><p></p></p><p><p></p></p><p><p> <strong><span style="font-family: Microsoft YaHei; font-size: 14px;">(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</span></strong></p></p><p><p><span style="font-family: Microsoft YaHei; font-size: 14px;"> <span style="color: #3366ff;"><strong>Interface three of the code:</strong></span></span></p></p><pre><pre>-(<span style="color: #0000ff;">void </span> <span style="color: #000000;">) viewdidload {[super viewdidload]; Secondviewcontroller </span> *SECONDVC = [self.navigationController.viewControllers Objectatindex:<span style="color : #800080; ">1 </span> <span style="color: #000000;">]; Viewcontroller </span> *FIRSTVC =<span style="color: #000000;"> [self.navigationController.viewControllers firstobject]; </span> <span style="color: #008000;">//</span> <span style="color: #008000;"> register notification </span> [nsnotificationcenter defaultcenter] ADDOBSERVER:SECONDVC selector: @selector (changebgcolor:) name:knotificationname <span style="color: # 0000ff; ">object </span> <span style="color: #000000;">:nil]; [[nsnotificationcenter defaultcenter] ADDOBSERVER:FIRSTVC selector: @selector (changebgcolor:) name: Knotificationname </span> <span style="color: #0000ff;">object </span> <span style="color: #000000;">:nil];} </span> </pre></pre><p align="left"><p align="left"><span style="font-family: Microsoft YaHei; font-size: 14px;">Ps:knotificationname is a string defined by a macro definition</span></p></p><p align="left"><p align="left"><span style="font-family: Microsoft YaHei; font-size: 14px;"></span></p></p><p align="left"><p align="left"><span style="font-family: Microsoft YaHei; font-size: 14px;"> <strong>(2) in the interface three click the color button, change the background color, and give the interface one and interface two to send notifications</strong></span></p></p><p align="left"><p align="left"><span style="font-family: Microsoft YaHei; font-size: 14px;"> <strong><span style="color: #3366ff;">Interface three of the code:</span></strong></span></p></p><pre><pre>-(ibaction) choosebgcolor: (UIButton *<span style="color: #000000;">) sender { </span>*colorarray =<span style="color: #000000;"> @[[uicolor redcolor],[uicolor bluecolor ],[uicolor greencolor],[uicolor purplecolor],[uicolor whitecolor],[uicolor blackcolor],[uicolor orangeColor],[ Uicolor Yellowcolor],[uicolor browncolor]]; </span> = [colorarray objectatindex:sender.tag-<span style="color: #800080;">1</span><span style="color: #000000;">]; </span> <span style="color: #008000;">//</span> <span style="color: #008000;">Raise Notification</span> <span style="color: #0000ff;">object</span>: [colorarray objectatindex:sender.tag-<span style="color: #800080;">1</span><span style="color: #000000;">]; }</span></pre></pre><p><p></p></p><p align="left"><p align="left"><strong><span style="font-family: Microsoft YaHei; font-size: 14px;">(3) processing notifications in interface one and interface two, changing the background color of the interface</span></strong></p></p><p align="left"><p align="left"><strong><span style="font-family: Microsoft YaHei; font-size: 14px;"> <span style="color: #3366ff;">Interface one and interface two in the Code:</span></span></strong></p></p><pre><pre>-(<span style="color: #0000ff;">void</span>) changebgcolor: (nsnotification *<span style="color: #000000;">) notification{ [self.view setbackgroundcolor:notification .</span> <span style="color: #0000ff;">Object</span> <span style="color: #000000;">];}</span></pre></pre><p><p><br><strong><span style="font-family: Microsoft YaHei; font-size: 14px;">(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</span></strong></p></p><p align="left"><p align="left"><strong><span style="font-family: Microsoft YaHei; font-size: 14px;"> <span style="color: #3366ff;">Interface three of the code:</span></span></strong></p></p><pre><pre>-(<span style="color: #0000ff;">void</span><span style="color: #000000;">) dealloc{ [[nsnotificationcenter defaultcenter] removeobserver:knotificationname];}</span></pre></pre><p><p></p></p><p><p><span style="font-family: ‘Microsoft YaHei‘; font-size: 15px;">To learn more about the small partners, you can click to <strong><span style="font-size: 18px;">view the source code</span></strong> , run the test yourself.</span></p></p><p><p><span style="font-family: ‘Microsoft YaHei‘; font-size: 15px;">Inquiries or technical exchanges, Please join the official QQ group: (452379712)<br></span></p></p><p><p></p></p>Jerry Education<br>Source:<span style="color: #000000;"><span style="color: #000000;"><span style="color: #000000;">http://www.cnblogs.com/jerehedu/</span></span></span><br><span style="font-family: ‘Microsoft YaHei‘; color: #000000;"><span style="font-family: ‘Microsoft YaHei‘; color: #000000;">This article is the copyright of <span style="color: #000000;">Yantai Jerry Education Technology co., Ltd.</span> and the blog Park is shared, welcome reprint, but without the consent of the author must retain this paragraph statement, and in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility. </span></span> <p><p> Use of the IOS nsnotification Center notification hub </p> </p></span>
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