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

28: Display different coloured versions of the same nib (xib) in the one window

Use NSViewController to display different custom views using the same nib and paint each one a different colour.

Problem: How do we know whether the displays generated by custom views using the same nib in the previous example in this series are independent of each other, i.e. that if I colour one of them red the other one will not also become red?

The adopted solution is to modify the previous code by adding to it a subclass of NSView which accepts a message that will cause it to repaint itself to a set of rgb colour values passed as parameters. The class of the ViewXIB is then set to this subclass and the message is sent from the main application object.

Implementation.

Create the file MyXibCustomView of type NSView.
Code it as shown below and save.

// MyXibCustomView.h #import <Cocoa/Cocoa.h> @interface MyXibCustomView : 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 // // MyXibCustomView.m // 027-window-many-same-nib // // Created by julius on 27/08/2008. // Copyright 2008 __MyCompanyName__. All rights reserved. // #import "MyXibCustomView.h" @implementation MyXibCustomView @synthesize floatRed; @synthesize floatGreen; @synthesize floatBlue; - (id)initWithFrame:(NSRect)frame { self = [super initWithFrame:frame]; if (self) { // Initialization code here. NSLog(@"MyXibCustomView initWithFrame"); } return self; } - (void) makeColourRed:(float)pRed green:(float)pGreen blue:(float)pBlue { self.floatRed = pRed; self.floatGreen = pGreen; self.floatBlue = pBlue; [self setNeedsDisplay:YES]; NSLog(@"MyXibCustomView makeColourRed Red=%5.3f green=%5.3f blue=%5.3f",pRed,pGreen,pBlue); } - (void)drawRect:(NSRect)rect { [[NSColor colorWithCalibratedRed:self.floatRed green:self.floatGreen blue:self.floatBlue alpha:1.0] set]; NSRectFill( rect ); } @end

Modify the awakeFromNib method of MyApplicationController.m so it looks as shown.

- (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; [(MyXibCustomView *)nsViewController1.view makeColourRed:1.0 green:0.0 blue:0.0]; [(MyXibCustomView *)nsViewController2.view makeColourRed:1.0 green:1.0 blue:0.0]; [nsViewController1.view setFrame:zFrame1]; [nsViewController2.view setFrame:zFrame2]; [nsWindowController.window.contentView addSubview:nsViewController1.view]; [nsWindowController.window.contentView addSubview:nsViewController2.view]; }

Save and Run.

Run the application

Run the application.

If you want to download the code

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

Download 028-window-many-coloured-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 drawing made from painting by Rubens of the Feast of Venus

animatedPaint