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:
(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:
// Created by Â«FULLUSERNAMEÂ» on Â«DATEÂ».
// Copyright Â«YEARÂ» Â«ORGANIZATIONNAMEÂ». All rights reserved.
self = [super initWithNibName:@"Â«FILEBASENAMEASIDENTIFIERÂ»"
// Initialize ivars not related to [self view] or XIB objects
- (id)initWithNibName:(NSString *)nibNameOrNil
return [self init];
// Init ivars that need anything from the XIB or [self view]
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!