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

4: TextField first responder and NextKeyView

Implement two simple text fields: NSTextField objects and set first responder.

In XCode: File->New Project->Cocoa Application

Call it: TextApp

Next do: File->New File and choose Objective C Class.

Call the files MyNSControl.h and MyNSControl.m, code them as shown below and save. Note these are subclassed as NSControl.

// MyNSControl.h // 004-TextFieldApp #import <Cocoa/Cocoa.h> @interface MyNSControl : NSControl { IBOutlet NSTextField *textField1; IBOutlet NSTextField *textField2; } - (IBAction)doText:(id)pId; - (IBAction)doText2:(id)pId; @end // MyNSControl.m // 004-TextFieldApp #import "MyNSControl.h" @implementation MyNSControl - (IBAction)doText:(id)pId; { NSString *tvarNSString = [pId stringValue]; NSLog(@"Hi There %@",tvarNSString); [pId setStringValue:@"Thank you"]; } // end doText - (IBAction)doText2:(id)pId; { NSString *tvarNSString = [textField2 stringValue]; NSLog(@"Hi There from text2 %@",tvarNSString); [textField2 setStringValue:@"Text 2 thank you"]; } // end doText2 @end

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

Drag two NSTextFields onto the Window panel. Click on the Window Panel (not the title bar) to select it.

In the Inspector chose the MyNSControl class. The actions doText and doText2 will appear in the action panel and the outlets textField1 and textField2 will appear in the outlets panel.

XCode, NSTextField create in Interface Builder

Interface Builder NSTextField create

Choose the Connections display in the Inspector.

Drag from the connection circle next to the textField1 outlet to the top textField. Do the same for textField2 and the bottom textField.

Drag from the connection circle next to the doText action to the top textField. Do the same for doText2 and the bottom textField as shown in the figure below.

Interface Builder, action and outlet connections from NSTextfield to NSControl

Interface Builder action and outlet connections from NSTextfield to NSControl

Click on the Window icon in the MainMenu.nib panel to select it.

Choose the Window Connections display in the Inspector.

Drag from the Initial First Responder outlet to the lower of the two text fields. This will give the focus to the lower field.

Interface Builder, setting First Responder

Interface Builder: set First Responder

Interface Builder, first responder has been set

Interface Builder: first responder has been set

Save Interface Builder.

Return to XCode. Choose Run->Console. Now Build and Run.

As you move the slider a sequence of 'Hi There value = ....' will appear in the Console log.

Go back into Interface Builder.

Click on the lower text field to select it (as TextField and not as TextFieldCell!).

In the connection pane of the inspector drag from next Key View to the top text field. Now select the top field and drag from next Key View to the bottom text field. This allows one to tab betwen fields.

Interface Builder, NSTextField nextKeyView

Interface Builder, NSTextField nextKeyView

Build and run.

Xcode, Interface Builder, NSTextField example running

Xcode, Interface Builder, NSTextField example running

Note that two IBOutlets have been defined, one for each of the text fields. These allow us to output to the text fields from anywhere in MyNSControl, i.e. without having to pass the id's obtained via the IBActions.

If you want to download the code

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

Download 004-TextFieldApp (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