IOS development-interface value passing (1)-Notification mode (broadcast)

Source: Internet
Author: User

The following blogs record common methods for passing values between different interfaces.

This article records the broadcast methods.

In the iOS design mode, the notification mode is also one of the important ones,
In fact, I think the broadcast mode is more appropriate.
It is used to synchronize an object to multiple objects.
The usage is very simple. When an object sends a broadcast, the listener needs to register it first, and then select the channel to listen to the broadcast content.

Note that you must register before listening. Otherwise, the sent broadcast cannot be accepted, that is, the value is changed and no response will be made.

The following is a simple demo with the following effects:

The first interface is used as the listener to register and listen to the broadcast.

The second interface is used as a broadcast to send broadcasts.

When the second interface sends a broadcast, the first interface automatically receives the broadcast.




Refer to the source code on git:

Https://github.com/colin1994/NotificationTest.git

The specific implementation is as follows:

On the first page, register as the listener and set the response event after receiving the broadcast:

-(Void) viewDidLoad {[super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. // We like to listen to ChangeTheme broadcasts // register as the listener of ChangeTheme channel of the broadcasting station nsicationicationcenter * nc = [NSNotificationCenter defacenter center]; // call self recvBcast once the listener has a broadcast: function [nc addObserver: self selector: @ selector (recvBcast :) name: @ "ChangeTheme" object: nil];} // This function is automatically called by the system. // the system automatically calls the ChangeTheme broadcast when the ios system receives the ChangeTheme broadcast. // broadcasting y is all the broadcast content-(void) recvBcast :( NSNotification *) notify {static int index; NSLog (@ "recv bcast % d", index ++); // obtain the broadcast content NSDictionary * dict = [notify userInfo]; NSString * name = [dict objectForKey: @ "ThemeName"]; UIColor * c = [dict objectForKey: @ "ThemeColor"]; self. title = name; self. view. backgroundColor = c ;}


Second Interface, send Broadcast

-(IBAction) btnClick :( id) sender {// obtain the globally unique NSNotificationCenter * nc = [NSNotificationCenter defacenter center] of the ios system. // set broadcast content NSString * name = @ "set color"; UIColor * color _ = [UIColor redColor]; NSDictionary * dict = [NSDictionary dictionaryWithObjectsAndKeys: name, @ "ThemeName ", color _, @ "ThemeColor", nil]; // encapsulate the content into the broadcast and send the broadcast to the ios system. // ChangeTheme channel [nc postNotificationName: @ "ChangeTheme" object: self userInfo: dict];}


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.