Quick Example of Multiple Corona Views in One iOS UIViewController

In case this helps anyone, just passing along a quick example of displaying multiple different CoronaView’s within a single UIViewController.

Within the iOS app bundle, I have these two folders that hold my Corona views…

  1. <bundle-path> / Corona / main.lua
  2. <bundle-path> / CustomChart / main.lua

Here is a snippet UIViewController and helper code that displays multiple CoronaViews.  You need a separate CoronaViewController for each CoronaView displayed (main.lua).  You also need to select the lua code path for each CoronaView using the ‘runWithPath:parameters’ method (as opposed to just using ‘run’).

All that said, the CoronaCards interface is very simple and intuitive…

#import "CoronaKit/CoronaKit.h" @interface MyViewController () @property (nonatomic, strong) CoronaViewController \*coronaController1; @property (nonatomic, strong) CoronaViewController \*coronaController2; @end @implementation MyViewController - (void)viewDidLoad { [super viewDidLoad]; // create the corona view controllers \_coronaController1 = [[[CoronaViewController alloc] init] autorelease]; \_coronaController2 = [[[CoronaViewController alloc] init] autorelease]; // add the first corona view (in bundle folder 'Corona') CoronaView \*coronaView1 = [self addCoronaCard:self.view.frame toController:\_coronaController1 usingFolder:@"Corona"]; // add the second corona view (in bundle folder 'CustomChart') CoronaView \*coronaView2 = [self addCoronaCard:self.view.frame toController:\_coronaController2 usingFolder:@"CustomChart"]; // send an event to either view (coronaView1 or coronaView2) } - (CoronaView\*)addCoronaCard:(CGRect)frame toController:(CoronaViewController \*)controller usingFolder:(NSString \*)folder { // add the given corona controller as a child to this view controller [self addChildViewController:controller]; // get the corona view CoronaView \*coronaView = (CoronaView \*)controller.view; // set the corona view frame coronaView.frame = frame; // add the corona view to this view controller's view hierarchy [self.view addSubview:coronaView]; // make the corona view's background transparent coronaView.backgroundColor = [UIColor clearColor]; coronaView.opaque = NO; // get the path for the given bundle folder NSString \*mainPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:folder]; // run the corona lua script contained within the given folder [coronaView runWithPath:mainPath parameters:@{}]; // return the new corona view return coronaView; } @end &nbsp;

If you want your CoronaView to have a transparent background, don’t forget to add this to each main.lua script needing transparency…

display.setDefault( "background", 0, 0, 0, 0 )

FYI, with the release of the latest CoronaCards beta, the header file in the example above should be changed from…

#import "CoronaKit/CoronaKit.h"

…to…

#import "CoronaCards/CoronaCards.h"

Looks rather helpful!

Thanks

Rob

FYI, with the release of the latest CoronaCards beta, the header file in the example above should be changed from…

#import "CoronaKit/CoronaKit.h"

…to…

#import "CoronaCards/CoronaCards.h"

Looks rather helpful!

Thanks

Rob