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

27: Display different renditions of the same nib (xib) in the one window

Use NSViewController to display different custom views using the same nib.

Problem: To create a number of custom views based on the same nib (xib) within the same window.

Implementation.

This code is a direct transposition of that presented to the Cocoa Dev list by Jerry Isdale under the heading creating and using a (Reusable) View xib. This version makes a small extension to the original and makes explicit several aspects of the code implied but not stated in the original.

Create a new project in XCode: File->New Project->Cocoa Application
Call it: 027-window-many-same-nib

Now create the classes: MyApplicationController of type NSObject, MyWindowController of type NSWindowController, MyViewXibController of type NSViewController.
Code these as shown below and save.

// MyApplicationController.h #import <Cocoa/Cocoa.h> @interface MyApplicationController : NSObject { IBOutlet NSWindowController *nsWindowController; NSViewController *nsViewController1; NSViewController *nsViewController2; } @end // MyApplicationController.m #import "MyApplicationController.h" #import "MyViewXibController.h" @implementation MyApplicationController - (id)init { if ( self = [super init] ) { // substitute yourView's nib + controller names in next line nsViewController1 = [[MyViewXibController alloc] initWithNibName:@"MyXibView" bundle:nil]; nsViewController2 = [[MyViewXibController alloc] initWithNibName:@"MyXibView" bundle:nil]; } return self; } - (void)awakeFromNib { //[nsViewController.view setFrame:[nsWindowController.window.contentView frame]]; NSRect zFrame1 = [nsViewController1.view frame]; NSRect zFrame2 = [nsViewController2.view frame]; zFrame1.origin.x = 10.0; zFrame2.origin.x = zFrame1.origin.x + zFrame1.size.width; [nsViewController1.view setFrame:zFrame1]; [nsViewController2.view setFrame:zFrame2]; [nsWindowController.window.contentView addSubview:nsViewController1.view]; [nsWindowController.window.contentView addSubview:nsViewController2.view]; } @end // MyWindowController.h #import <Cocoa/Cocoa.h> @interface MyWindowController : NSWindowController { } @end // MyWindowController.m #import "MyWindowController.h" @implementation MyWindowController @end // MyViewXibController.h #import <Cocoa/Cocoa.h> @import "MyXibCustomView.h" @interface MyViewXibController : NSViewController { } @end // MyViewXibController.m @implementation MyViewXibController @end

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

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

Drag two NSObjects onto the mainMenu.xib window and set their classes to MyApplicationController and MyWindowController.

Select MyApplicationController and in the Connections pane of the Inspector link MyApplicationController's nsWindowController outlet to MyWindowController.

Select MyWindowController and in the Connections pane of the Inspector link its window outlet to the Window.

Save the xib.

XCode,Interface Builder - Link app controller to the window controller

Link MyApplicationController outlet to MyWindowController

XCode,Interface Builder - Link the window controller to the window

Link MyWindowController to the window

Bring up the MyXibView.xib file by double clicking it.

Customise the view as shown. The button and field are there solely to show the location of the views when they are attached to the main application window shown in MainMenu.xib.

Select File's Owner by double clicking it.

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

In the Connections pane of the Inspector link its view outlet to the CustomView

Save and Run.

Customise the view

Customise the view. The button and text field will show the view's location in the main window.

Set the class of the File's Owner

Interface Builder: Set the class of the File's Owner

Link the File's Owner to the custom view

Link the File's Owner to the custom view

The runtime window. Note the two custom views displayed side by side

The runtime window. Note the two custom views displayed side by side

If you want to download the code

Click the Download Link to obtain 027-window-many-same-nib.zip file of this whole OS X 10.5 Leopard program.

Download 027-window-many-same-nib.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 using new technique to represent looking through branches of a weeping willow by a river

animatedPaint