The XIB file format is introduced in the Interface Builder 3 on Leopard. It replaces the previous file format NIB, short for NeXT Interface Builder. The Interface Builder application is a GUI for building and assembling user-interface objects, images and references to sounds for your Mac application. It allows you to drag and drop user-interface objects such as buttons, tableviews onto a window and layout the objects visually. These objects are archived and saved as a XIB file. When an application launches, these XIB files are loaded and un-archived. The user-interface objects are then made available to the application.
For those who are keen to know more about the inner workings of XIB files, I would recommend checking out Fraser Speirs' post on XIB files.
Using the Interface Builder is a convenient way to handle views hierachy and user-interface objects but however, you can also opt to handle these programmatically. To do that, create a new Project in XCode and use the "Window Based Application" template. This template is the basic and simplest template and it is ideal for this purpose.
The template provides the MainWindow.xib file which is unnecessary since we are programatically creating the user-interface objects. You can delete it and also remove the reference to this XIB file by deleting the last entry "Main nib file base name" in the Info.plist file.
You would then edit the main.m file as shown below:
int main(int argc, char *argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int retVal= UIApplicationMain(argc,argv,@"UIApplication",@"AppDelegate");
[pool release];
return retVal;
}
The main.m file, as the name suggests, contains the code that will run when the application gets launched. By editing the main.m file as shown, the code will create an instance of a UIApplication class named "AppDelegate".
The last step to this would be to set up the AppDelegate class:
#import "AppDelegate.h"
@implementation AppDelegate
@synthesize window;
-(void)applicationDidFinishLaunching:(UIApplication *)application {
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
window.backgroundColor = [UIColor whiteColor];
// Override point for customization after application launch
[window makeKeyAndVisible];
}
-(void)dealloc {
[window release];
[super dealloc];
}
@end
All that the AppDelegate class does is to create the main window of the application and make it visible. Voila! Now you are all set to go! A basic iPhone App without any XIB file.
What others think
Jeffrey Sharp
Nov 05 2008
This is great. In my current iPhone app (in progress), it seems like using XIBs adds more complexity than there needs to be. I'm already doing so much custom drawing that it is seems simpler to just handle the UI 100% programmatically.
I would think the programmatic method performs better as well. Any benchmarks?
BigEvilEmpire
Nov 05 2008
Unfortunately I don't have any benchmark for that. I find that the programmatic approach is often more flexible and faster with custom UI in certain scenarios.