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

6: NSTextView with delegate

Implement simple NSTextView object and a delegate.

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

Now create the class: MyDelegate of type NSObject.
Code it as shown below and save.
Note that
-(void)textDidChange:(NSNotification *)pNotification
is a message sent to delegates by NSTextView

// MyDelegate.h // 006-TextView-Delegate #import <Cocoa/Cocoa.h> @interface MyDelegate : NSObject { } @end // MyDelegate.m // 006-TextView-Delegate #import "MyDelegate.h" @implementation MyDelegate - (void)textDidChange:(NSNotification *)pNotification { NSLog(@"Hi there"); } // end textDidChange @end

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

Drag an NSTextView from the IB Library onto the Window panel. Click on the view to select it. In the class field in the Text Identity section of the Inspector select NSTextView.

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 MyDelegate.

XCode,Interface Builder, drag NSObject onto MainMenu.nib

Interface Builder drag NSObject onto MainMenu.nib

In the main window click on NSTextView to select it.
Bring up the Connections section of the Inspector.
Drag from the circle next to the delegate entry to MyDelegate in the MainWindow.nid window.

XCode,Interface Builder, define MyDelegate as NSTextView delegate

Interface Builder, define MyDelegate as NSTextView delegate

Interface Builder, delegate link from NSTextView to delegate

Interface Builder, delegate link from NSTextView to delegate

Save Interface Builder.
Go back to XCode and run.
Whenever a key is pressed 'Hi there' is shown in the Log.

XCode run the application

XCode run the application

If you want to download the code

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

Download 006-TextView-Delegate.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

animatedPaint