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

5: NSTextView get keyDown events

Implement simple NSTextView object and respond to keyDown events.

This example is a highly pared down version of the program provided by http://hayne.net/MacDev/TestKeyDownEvent/

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

Now create two classes: MyController of type NSObject and MyTextView of type NSTextView.
Code these as shown below and save.

// MyController.h // 005-TextView #import <Cocoa/Cocoa.h> @interface MyController : NSObject { IBOutlet id textView; } -(void)reactToKeyDownEvent:(NSEvent *)pEvent; @end // MyController.m // 005-TextView #import "MyController.h" @implementation MyController -(void)reactToKeyDownEvent:(NSEvent *)pEvent; { NSLog(@"Hi there"); } // end reactToKeyDownEvent @end // MyTextView.h // 005-TextView #import <Cocoa/Cocoa.h> @interface MyTextView : NSTextView { IBOutlet id theController; } @end // MyTextView.m // 005-TextView #import "MyTextView.h" #import "MyController.h" @implementation MyTextView - (void)keyDown:(NSEvent *)pEvent { [theController reactToKeyDownEvent: pEvent]; [super keyDown: pEvent]; } @end

Now bring up Interface Builder by double clicking on MainMenu.nib in the Resources section of SliderApp

Drag an NSTextView from the IB Library onto the Window panel. Click on the view to select it. The class field in the Text Identity section of the Inspector will show NSTextView and MyTextView.
Choose MyTextView.
In the outlets part of the Inspector window you will now see 'theController'.

XCode,Interface Builder - drag NSTextView onto window

Interface Builder: drag NSTextView onto window

From the Library drag an NSObject onto the MainMenu.nib window.
Select it and in the pop-up menu of the Class field of the Identity section of the Inspector select MyController.

Bring up the Connections section of the Inspector.
You will see textView under the 'outlets' heading.

XCode,Interface Builder, drag NSObject onto MainMenu.nib

Interface Builder drag NSObject onto MainMenu.nib

Select MyTextView.
In the Connections section of the Inspector drag from the 'theController' outlet to the MyController icon in MainMenu.nib.

Interface Builder, drag from NSTextView 'theController' outlet to MyController (NSObject)

Interface Builder drag from NSTextView 'theController' outlet to MyController (NSObject)

The NSTextView 'theController' outlet is linked to theController.

Interface Builder, NSTextView 'theController' outlet is linked to theController

Interface Builder NSTextView 'theController' outlet is linked to theController

Select MyContoroller in the MainMenu.nib window and then control-drag from MyController to MyTextField in the Window window.
The Connections sections of the Inspector will now show textView connected to MyTextView.

Interface Builder, drag from MyController (NSObject) in MainMenu.nib to NSTextfield

Interface Builder drag from MyController (NSObject) in MainMenu.nib to MyTextfield

A pop-up menu appears.
Select textView.

Interface Builder, select textView from pop-up menu in Window

Interface Builder select textView from pop-up menu in Window

The MyController textView outlet is now linked to the NSTextView.

Interface Builder, MyController outlet is now linked to the NSTextView

Interface Builder: the MyController textView outlet is now linked to the NSTextView

Select MyTextView in the Window window.
In the Connections sections of the Inspector drag from the round circle of the theController outlet to the MyController icon in the MainMenu.nib window.
The inspector will now show theController to be connect to MyController.

Save IB. Go back to XCode. Build and run.
For each character you type in the TextView so you will see the greeting 'Hi there' appear in the Console.

XCode run the application

XCode run the application

If you want to download the code

Click the Download Link to obtain 005-TextView.zip file of this whole OS X 10.5 Leopard program.

Download 005-TextView.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 Sketch for paintings of the genocide in Rwanda represented as a large Falla in the Passeo de las Germanias in Gandia

animatedPaint