Separate nibs/xibs Part 2

Following the previous post that I explained you why you are encouraged to separate your nib/xib files now I am going to explain you how to do it.

STEP #1 The window controller

First we need to create the window controller (NSWindowController) :)  The header file AboutWindowController.h

#import <cocoa /Cocoa.h>
@interface AboutWindowController : NSWindowController { }
@end</cocoa>

And the implementation file AboutWindowController.m

#import "AboutWindowController.h"
@implementation AboutWindowController
- (id)init {
	if ((self = [super initWithWindow:NULL]) != NULL) {}
	return self;
}
- (NSString *)windowNibName {
	return NSStringFromClass([self class]);
// This determines the name of the Xib file, In this case the name
// of the class, so we need to create the AboutWindowController.xib
}
@end

STEP #2 Create the Xib

Now, with Interface Builder we need to add a new Application Xib and name it “AboutWindowController”. Step 1 Step 2

Click here to read more »

Separate nibs/xibs

In case you are wondering why you should separate your nibs/xibs in your project you are in the right post. The Apple resource programming guide explains everything in detail but we are after the following paragraph:

“In the main nib file for a Mac OS X application, consider storing only the application menu bar and an optional application delegate object in the nib file. Avoid including any windows or user-interface elements that will not be used until after the application has launched. Instead, place those resources in separate nib files and load them as needed after launch.” (link)

Having everything in one nib is in general a bad idea. The reason is that you are instantiating everything at once when the application is launching. Of-course in a tiny application you wont even notice a difference in the performance neither in the way you organise the project but when the project is expected to be big you are encouraged to separate it.

For example if you have everything in one nib/xib file you are instantiating the preferences window, about window and other windows and controllers that you may not using.

You should be using multiple nibs.