Use of IOS NSNotification Center notification Center, iosnsnotification

Source: Internet
Author: User
Tags notification center

Use of IOS NSNotification Center notification Center, iosnsnotification

Notification center, which is a message broadcast mechanism inside the IOS program. Through it, communication between objects without reference relationships can be realized. The notification center is based on the observer mode. It can only communicate within the program and cannot communicate across application processes. When the notification center receives a message, it sends the message to the subscriber according to the settings. There can be multiple subscriber here.

Similar to the proxy mode, the notification center can communicate with multiple objects. The notification center can send a notification to multiple listeners, while the proxy mode can only add one proxy for each object. However, either the mode is a low-coupling design that enables communication between objects.

Procedure for using the notification center

1. register the observer to be interested in an event (named as a string) and set the Selector or Block to be executed when the event is triggered.

2. nsicationicationcenter fires an event at a certain time point (named as a string)

3. When the observer receives an event of interest, it executes the corresponding Selector or Block

4. Removal notification

Notification center case

Add a group of buttons on the third page by using navigation. When you click the button, set the background color of the current page to the button color and send a notification, set the background color of the previous two interfaces to the selected color, program framework, and interface.

 

  (1) register the notification. In the viewDidLoad method of Interface 3, register the notification for interface 1 and interface 2. When sending the notification, the corresponding method will be called in interface 1 and interface 2.

  Code 3 in the interface:

-(Void) viewDidLoad {[super viewDidLoad]; SecondViewController * secondVC = [self. navigationController. viewControllers objectAtIndex: 1]; ViewController * firstVC = [self. navigationController. viewControllers firstObject]; // registration notification [[nsicationcenter center defacenter center] addObserver: secondVC selector: @ selector (changeBgColor :) name: kNotificationName object: nil]; [nsicationicationcenter defacenter center] addObserver: firstVC selector: @ selector (changeBgColor :) name: kNotificationName object: nil];}

Ps: kNotificationName is a string defined by a macro.

 

  (2) When you click the color button on page 3, change the background color and send notifications to page 1 and page 2.

  Code 3 in the interface:

-(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]; // trigger a notification [[nsnotifcenter center defacenter center] postNotificationName: kNotificationName object: [colorArray objectAtIndex: sender. tag-1];}

 

(3) process the notification in interface 1 and interface 2 and change the background color of the interface.

Code 1 and 2:

-(void)changeBgColor:(NSNotification *)notification{    [self.view setBackgroundColor:notification.object];}


(4) Remove a notification on Interface 3. Generally, the dealloc method of the parent class is overwritten. However, in the arc mode, the dealloc method cannot call the dealloc method of the parent class.

Code 3 in the interface:

-(void)dealloc{     [[NSNotificationCenter defaultCenter] removeObserver:kNotificationName];}

 

For more information, clickView Source CodeRun the test in person.

For questions or technical exchanges, please join the official QQ group: (452379712)

 

Author: Jerry Education
Source: http://www.cnblogs.com/jerehedu/
The copyright of this article belongs to Yantai Jerry Education Technology Co., Ltd. and the blog Park. You are welcome to repost it. However, you must keep this statement without the author's consent and provide the original article connection on the article page, otherwise, you are entitled to pursue legal liability.

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.