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

18: Communication between controller and custom NSView using IBOutlet

Communication with Custom NSView using IBOutlet.

The problem: you have a controller and you want it to send messages to a Custom NSView.

Solution: create an IBOutlet in the controller then in Interface Builder drag from the IBOutlet to the Custom View. This sets the IBOutlet to the id of the Custom View object.


Create a new project in XCode: File->New Project->Cocoa Application
Call it: 018-IBOutlet-Custom-NSView

Now create the classes: MyControllerView of type NSView and MyCustomView of type NSView.
Code these as shown below and save.

// MyControllerView.h // 018-NSButton-NSView // #import <Cocoa/Cocoa.h> #import "MyCustomView.h" @interface MyControllerView : NSView { // something completely obvious (oh yeah?) that took me six months to discover. // To get the id of a custom view into a controller object declare an outlet // then in Interface Builder->Inspector->connections->Outlets you will see // that outlet. If you now drag from this to the custom view.... // the id of the custom view will get put in there // - yes it will, it really will!!! IBOutlet MyCustomView *myCustomViewObjOutlet; IBOutlet NSTextField *myTextFieldOutlet; } //- (IBAction)doText:(id)pId; - (IBAction)assignCustViewString:(id)pId; - (IBAction)readCustViewString:(id)pId; @end // MyControllerView.m // 018-NSButton-NSView // #import "MyControllerView.h" @implementation MyControllerView // this is never called because it, together with its paper texture was created in IB - (id)initWithFrame:(NSRect)frame { if ((self = [super initWithFrame:frame]) == nil) { NSLog(@"MyControllerView initWithFrame failed"); return self; } // end if return self; } - (IBAction)assignCustViewString:(id)pId; { [myCustomViewObjOutlet setMyString:[myTextFieldOutlet stringValue]]; } // end readCustViewString - (IBAction)readCustViewString:(id)pId; { // Note: setStringValue is inherited from NSControl [myTextFieldOutlet setStringValue:[myCustomViewObjOutlet myString]]; } // end readCustViewString - (void)drawRect:(NSRect)pNSRect { } @end // MyCustomView.h // 018-NSButton-NSView // #import <Cocoa/Cocoa.h> @interface MyCustomView : NSView { NSString *myString; } -(NSString *)myString; -(void)setMyString:(NSString *)pString; @end // MyCustomView.m // 018-NSButton-NSView // #import "MyCustomView.h" @implementation MyCustomView - (id)initWithFrame:(NSRect)frame { if ((self = [super initWithFrame:frame]) == nil) { NSLog(@"MyCustomView initWithFrame failed"); return self; } // end if myString = @"MyCustomView Saved String"; return self; } -(NSString *)myString; { return myString; } // end myString -(void)setMyString:(NSString *)pString; { // Raises an NSInvalidArgumentException if pString is nil. if(pString) { myString = [[NSString alloc]initWithString: pString]; } else { myString = @""; } // end if } // end setMyString - (void)drawRect:(NSRect)pNSRect { [[NSColor greenColor] set]; NSRectFill( pNSRect ); } @end

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

Drag a Custom NSView, two NSButtons and an NSTextField onto the window.

Click on the window's NSView to select it.
Set the class to MyControllerView and save.

XCode,Interface Builder - set the class of the window's NSView

Interface Builder: set the Class of the window's NSView

Link them as shown.

XCode,Interface Builder,Link controller to IBActions and IBOutlets

Link the controller to IBActions and IBOutlets

Go into XCode and run.

XCode,Interface Builder, Run IBOutlet NSView


If you want to download the code

Click the Download Link to obtain 018-NSButton-NSView.zip file of this whole OS X 10.5 Leopard program.

Download 018-NSButton-NSView.zip (2.3 MB)

Useful Links

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

  • painting after Leonardo Da Vinci's Battle of Anghiari