Example code programming Objective-C with Cocoa in Xcode and Interface Builder (Leopard)

29: Use the same nib to create different windows

Create different windows using the same nib (xib).

Problem: To create different windows having the same compoents and structure.

Solution: Create a window having the required components and structure as a separate xib (nib) file. To create a new window create a new instance of the nib's File's Owner Class.

Here is the 2.3MB source of this OS X 10.5 Leopard program.


XCode,Interface Builder - A sequence of windows created from same nib /xib file

A sequence of windows created from same nib/xib file

Create a new project in XCode: File->New Project->Cocoa Application
Call it: 029-multi-windows

Now create the classes: MyAppControl of type NSObject,
MyWindowController of type NSWindowController,
MyCustomView of type NSView.
Code these as shown below and save.

MyWindowController will be the Nib File's Owner class and MyCustomView is the class of the Nib's window view.
MyAppControl will create the new instances of MyWindowController in response to a button click from a button on a time application window.

// MyAppControl.h // Created by julius on 27/08/2008. #import <Cocoa/Cocoa.h> @interface MyAppControl : NSObject { NSMutableArray *nsMutaryOfWindows; NSPoint nsPointWindowOrigin; } - (IBAction)createWindow:(id)pId; @end // MyAppControl.m #import "MyAppControl.h" #import "MyWindowController.h" #import "MyCustomView.h" @implementation MyAppControl - (void)awakeFromNib { nsMutaryOfWindows = [[NSMutableArray alloc]init]; nsPointWindowOrigin.x = 30.0; nsPointWindowOrigin.y = 20.0; } // end awakeFromNib - (IBAction)createWindow:(id)pId { MyWindowController *zWindowController = [[MyWindowController alloc] initWithWindowNibName:@"MyExtraWindow"]; nsPointWindowOrigin.x += 20.0; nsPointWindowOrigin.y += 15.0; [zWindowController.window setFrameOrigin:nsPointWindowOrigin]; [(MyCustomView *)[zWindowController idWidowCustomView] makeColourRed:(nsPointWindowOrigin.x/1200.0) green:(nsPointWindowOrigin.y/400.0) blue:(1.0 - ((nsPointWindowOrigin.x/600.0)))]; [zWindowController showWindow:self]; [nsMutaryOfWindows addObject:zWindowController]; } // end @end // MyWindowController.h #import <Cocoa/Cocoa.h> @interface MyWindowController : NSWindowController { IBOutlet id idWidowCustomView; } @property id idWidowCustomView; @end // 029-multi-windows #import "MyWindowController.h" @implementation MyWindowController @synthesize idWidowCustomView; @end // MyCustomView.h #import <Cocoa/Cocoa.h> @interface MyCustomView : NSView { float floatRed; float floatGreen; float floatBlue; } @property float floatRed; @property float floatGreen; @property float floatBlue; - (void)makeColourRed:(float)pRed green:(float)pGreen blue:(float)pBlue; @end // MyCustomView.m #import "MyCustomView.h" @implementation MyCustomView @synthesize floatRed; @synthesize floatGreen; @synthesize floatBlue; - (id)initWithFrame:(NSRect)frame { self = [super initWithFrame:frame]; if (self) { // Initialization code here. } return self; } - (void) makeColourRed:(float)pRed green:(float)pGreen blue:(float)pBlue { self.floatRed = pRed; self.floatGreen = pGreen; self.floatBlue = pBlue; [self setNeedsDisplay:YES]; } - (void)drawRect:(NSRect)rect { [[NSColor colorWithCalibratedRed:self.floatRed green:self.floatGreen blue:self.floatBlue alpha:1.0] set]; NSRectFill( rect ); } @end

Create the xib file MyExtraWindow.xib of type ViewXIB (XCode File->New File->Cocoa->ViewXIB).

Bring up Interface Builder by double clicking on MainMenu.nib.

Drag an NSObject onto the mainMenu.xib window and set its class to MyAppControl.

Customise the application window as shown and in the Connections pane of the Inspector link its window outlet to the Window.

Save the xib.

XCode,Interface Builder - Link MyAppControl to the button

Link MyAppControl to the button for creating the windows

XCode,Interface Builder - Links from the File's Owner

The new window's nib, xib: link the File's Owner to the Window and it's idCustomView outlet to the window's NSView component.

Double click MyExtraWindow.xib to bring it up.

Customise the view as shown.
The button and field are there solely for decoration.

Click on the window's background to select it's view. In the Identity pane of the Inspector set its class to MyCustomView.

Select File's Owner by double clicking it.

In the Identity pane of the Inspector set its class to MyWindowController.

In the Connections pane of the Inspector link its window outlet to the Window, and link the idCustomView outlet to the custom view

Save and Run.

If you want to download the code

Click the Download Link to obtain 029-multi-windows.zip file of this whole OS X 10.5 Leopard program.

Download 029-multi-windows.zip (2.3 MB)

Please send me your comments

If you include your e-mail I may reply!  

Page last modified: 18:58 Sunday 12th. May 2013

Julius Guzy

Paintings & Drawings

  • Link to picture about the israeli and palestinian conflict entitled: how we pacified our genocidal neighbours