Xcode 6: Removing Storyboards and Creating Useful Empty Projects

Xcode 5 and 6 introduced some changes in iOS project templates, which may or may not be to everyone’s taste. This mini-tutorial shows how to create a pre-Xcode 5-style Single View Application based on xib files, and and a pre-Xcode 6-style Empty Application, using Xcode 6.

Prior to the introduction of Xcode 5, creating a new iOS project from a built-in template got you a skeleton project based on explicit xib files. For example, selecting “Single View Application” created an Application Delegate, a View Controller, and a corresponding xib file:

Xcode 4 Single View Application

Selecting “Empty Application” created yielded a project with just an Application Delegate, whose only function was to display an empty white window. This sort of project is useful for starting from a nearly absolute minimum, and has some pedagogical value, notably in the Big Nerd Ranch’s iOS Programming: The Big Nerd Ranch Guide, where they start off new projects from the Empty Application template.

With the introduction of Xcode 5, the iOS templates were changed to favor Storyboards. With the introduction of Xcode 6, the Empty Application template has been replaced with an Empty project template, which constructs a project containing no files or targets whatsoever.

Here, for example, is what Xcode 6 gives you for a Single View Application:

Xcode 6 Single View Application

You get not only a Main.storyboard file, but also a default LaunchScreen.xib file. Some developers prefer Storyboards, and some do not. It’s relatively easy to modify a project to get back to what a pre-Xcode 6 Empty Application template created; a second modification gets you from there to a “standard” xib-based application, à la Xcode 4. (It should be possible to create custom Xcode 6 templates for both of these types of projects, but that will require some research and experimentation).

The first step is to remove Main.storyboard, LaunchScreen.xib, ViewController.h, and ViewController.m—select them all, right-click, and choose to either remove them from the project, or delete them completely:

Xcode 6 Remove Files

If you run the app at this point, you’ll get an exception: “Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: ‘Could not find a storyboard named ‘Main’ in bundle…” This is because the project is trying to use that storyboard file you just removed. This information is in the project’s Info.plist, and the entry is “Main storyboard file base name”:

Xcode 6 Fix PList

To fix this, simply remove the entry (select it and hit delete or hover and click the remove icon). Running the app now will just get you a solid black window, because there’s simply no code present that will create one for you. We’re just one step away from the equivalent of an Xcode 5 Empty Application. Open AppDelegate.m, and edit applicationDidFinishLaunchingWithOptions so that it looks like this:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

Running the app at this point will get you a white window, just as it did in Xcode 4 and 5. As well, you’ll see a warning in the output window in Xcode:

Xcode 6 Root View Controller Warning

which also appeared in Xcode 5’s Empty Application, by the way.

At this point, we’re functionally equivalent to an Xcode 5 (or 4) Empty Application. Again, this is the “starting point” used in the Big Nerd Ranch’s iOS programming book. In order to get to the functional equivalent of an Xcode 4 Single View Application, we need to add a View Controller and its corresponding xib file, and hook them up. The first step is to add a subclass of UIViewController and a corresponding xib file. Use ⌘-N or the File->New->File.. menu item to create a new file, and select “Cocoa Touch Class”:

Xcode 6 Add View Controller 1The next dialog asks you to name the class; I cleverly called mine “ViewController”. Make it a subclass of “UIViewController”, and check the “Also create XIB File”:

Xcode 6 Add View Controller 2This will create three files in your project: ViewController.h, ViewController.m, and ViewController.xib:

Xcode 6 Add View Controller 3

Running the app at this point will give you the same behavior as before, as we’ve not yet told your app to actually use the ViewController. To do this, we need to add a property to the AppDelegate, to hold the ViewController; edit AppDelegate.h so it looks like this:

#import <UIKit/UIKit.h>

@class ViewController;

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@property (strong, nonatomic) ViewController *viewController;

@end

Finally, we need the AppDelegate to create and make use of the ViewController. Add an import directive to AppDelegate.m, so that it knows about the ViewController:

#import "AppDelegate.h"
#import "ViewController.h"

and then edit didFinishLaunchingWithOptions  so it looks like this:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}

Instead of simply making the window background white, we tell the app to make us a ViewController from the new xib file, and use it as the root view controller (thereby eliminating the warning we saw earlier). At this point, we have what Xcode 4’s Single View Application template created: a minimal, non-storyboard-based app with one xib file.

3 thoughts on “Xcode 6: Removing Storyboards and Creating Useful Empty Projects”

Comments are closed.