Xcode Templates

Joe Conway's Headshot
Joe Conway

Aaron Hillegass' last post on initializers for UIViewController was spot-on. In fact, when you come to our iPhone Bootcamp, you'll learn all about it.

However, the extra typing every time we create a file is a nuisance. When you create a subclass of a UIViewController, chances are you really want to get to work on the important stuff. In this post, I'll show you how to set up your Xcode template for UIViewController subclasses with a XIB file so that you don't have waste any time creating your next iPhone app.

Currently, Xcode templates are located at:

/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/

(I say currently because there is no guarantee the next major version of Xcode will look for templates there. You are safe for now, though.)

What we want to do is change the template for creating UIViewController subclasses. That template is located here:

/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/File Templates/Cocoa Touch Class/UIViewController subclass

Quick Finder Tip: When the Finder is active, hit Cmd-Shift-G. Copy and paste the previous path in to the sheet that appears and hit Go. If you installed the Developer tools to another directory, you'll have to replace /Developer/ with that path. Optionally, you can spotlight for FILEBASENAMEASIDENTIFIER and it will point you within the vicinity. (FILEBASENAMEASIDENTIFIER is the keyword in Xcode templates that gets replaced by the name of the file you are prompted to enter.)

In this directory, there is a property list and two directories. (For the more curious, you can open up the property list for some interesting Xcode hacking.) The directory we are interested in is UIViewController subclass with XIB.pbfiletemplate. Open that directory.

From that directory, open class.m in Xcode. Mine looks like the following: ``

//
//  «FILENAME»
//  «PROJECTNAME»
//
//  Created by «FULLUSERNAME» on «DATE».
//  Copyright «YEAR» «ORGANIZATIONNAME». All rights reserved.
//

«OPTIONALHEADERIMPORTLINE»

@implementation «FILEBASENAMEASIDENTIFIER»

- (id)init
{
    self = [super initWithNibName:@"«FILEBASENAMEASIDENTIFIER»" 
                           bundle:nil];
    // Initialize ivars not related to [self view] or XIB objects

    return self;
}
- (id)initWithNibName:(NSString *)nibNameOrNil 
               bundle:(NSBundle *)nibBundleOrNil 
{
    return [self init];
}

- (void)viewDidLoad 
{
    [super viewDidLoad];
    // Init ivars that need anything from the XIB or [self view]    
}

- (void)viewDidUnload 
{

}

- (void)dealloc 
{
    [super dealloc];
}

@end

You can copy and paste this text in to that file and save it or roll your own. Now you can imbibe Aaron's wisdom without having to do any real work!

Recent Comments

comments powered by Disqus