Integrating Cocos2d and Storyboards

I have read quite a few tutorials on integrating Cocos2d and UIKit. They all worked great until you wanted to have a portrait mode UIKit view and a landscape view cocos2d game.  This tutorial will show you how to integrate Storyboards and Cocos2d. The initial screens will be in portrait mode using Storyboards and tapping on a button with take you to the cocos2d view which is in landscape mode. This tutorial does not show you how to integrate cocos2d and UIKit on the same view. To see some other tutorials that may help you check out

To start with I am running Xcode 4.3.2 and I am using cocos2d-iphone-1.0.1.

My assumption is that you have already installed cocos2d and if you are using storyboards you have to target iOS 5 and above.

Begin by creating a new cocos2d project.

Xcode->File->New->Project->IOS->cocos2d->cocos2d, select next and call your project TestCocos2dUIKit, click Next, choose where you want to save your project and click the create button.

Run the project and you you see the familiar "Hello World" screen.

We now want to create some views using Storyboards.  We are going to have the first view as a portrait UIViewController and we will add two buttons. The first button will take you to another UIViewController and the second button will take the user to the "game", which will be a cocos2d view in landscape mode.

In Xcode go to File->New->File->User Interface->Storyboard.  Click next and on the next screen for the device family choose iPhone.  Click next and enter a name for the storyboard. Name the storyboard "MainStoryboard_iPhone".  Click the create button.

Make sure you have both the left and right panels visible, by selecting the appropriate buttons as shown in Figure 1 A.
Now select the MainStoryboard_iPhone from the left hand side of Xcode. Figure 1 B.  The storyboard editor is now visible.
The next step is to drag and drop a UIViewController from the right side panel that has all the objects.  Figure 1 C.
We are going to need a Navigation Controller so from the menu at the top select Editor ->Embed In->Navigation Controller.  Your screen should look like Figure 1.
Now add two buttons to the view controller by dragging them from the Objects Panel onto the view controller.  It does not matter where you place them on the view, but change the title for the one button to "UIKit" and title the other button "Cocos2d".
The next step is to add another view controller.  Drag the view controller from the Objects Panel onto the Storyboard just as you did for the first view controller. Now add a label from the Objects Panel onto the newly added view controller.  Change the text of the label to "UIKit View".  Repeat this step and drag another view controller onto the storyboard and add a label to this view controller too.  This time change the label to be "Cocos2d View".

We now need to to modify the view controller with the "Cocos2d View" label to use the RootViewController, which was generated by the Cocos2d template.  Select the view with the "Cocos2d View" Label so that it is highlighted in blue as shown in Figure 2.  Select the Identity Inspector and change the class to be RootViewController as shown in Figure 2.  The RootViewController was being used by the AppDelegate to launch the Cocos2d OpenGL View. We are now going to run this code when this view controller is displayed.
The last step in the storyboards is to wire up the buttons.  Select the UIKit button in the first view controller we added.  While holding down the Control button drag the mouse over to the view with the "UIKit" label.  Release the mouse and select "Push" from the Storyboard Segues.  Repeat the process for the "Cocos2d" button. Select the Cocos2d button, press and hold the Control button while you drag the mouse over to the Root View Controller (The view with the "Cocos2d View" label.) Once again select the Push option from the Storyboard Segue.  I was unable to capture a screenshot of this step.  Hopefully you manage to figure it out.

When the RootViewController is selected we need to initialize the cocos2d environment and display the cocos2d view.

Select RootViewController.m and add the following code somewhere in the file.
This code is very similar to the the applicationDidFinishLaunching code in the AppDelegate with a few changes that are needed to run it within a view.

We now need to get the app to use the Storyboard.

Select the AppDelegate file and you need to modify applicationDidFinishLaunching as shown below.
The initialization code for cocos2d is now in RootViewController and the app will now look in Info.plist file for a NIB or Storyboard to load.  We are going to tell it it load the storyboard.

Select the Info.plist file from the left hand file panel. Then in the central editor panel right click and select Add Row.

For the Key type in "Main storyboard file base name (iPhone)"  Then for the value type in the name of the storyboard which is "MainStoryboard_iPhone" See figure 3.

All the changes have been made, so compile and run the App.

A tap on the UIKit button will take you to the UIKit View.  A tap on the Cocos2d button will take you to the Hello World View.

The code can be downloaded here.
I hope this tutorial was useful for you.