Chp 4 Challenge Solution

Friday, September 16, 2005

The challenge was to create a letter counting application.

So let's start out by launching Xcode, and creating a new project. Since the project is only one simple window, select a Cocoa Application as the project type and name the project "LetterCounter".

In the project, open the MainMenu.nib file.

Add two NSTextField controls, and one NSButton control to the window. Adjust the layout as shown in the book, or make up your own if you'd like. You will also want to remove any default text that is in your result field at this point, since it will be there when the application displays the window.

Once we've got things laid out, we need to add an object to handle the work of counting the letters and displaying the results.

To do this, go to the MainMenu.nib window containing your nib object instances. Click the Classes tab to view the classes currently available in your nib. Now, select the NSObject class in the first level of the hierarchy. (First column if viewing in standard column browser view, otherwise its the first level of the tree view.) Now, subclass NSObject into a new class named "AppController". Of course the name doesn't really matter, but AppController seems like the traditional name for this type of stuff.

Now that we have a class to act as the controller, we need to make it aware of the input field, and the result field we have in the window. With the AppController selected, go to the Classes menu and select "Add Outlet to AppController". You should see the inspector window showing the new outlet. Change the name of the outlet to 'inputTextField', and the type should be NSTextField. Next, add a second outlet with a type of NSTextField named 'resultTextField'.

In the inspector window select the outlet tab item and add a new action named 'countLetters'. Once you have the two outlet and one action defined. Go back the MainMenu.nib window, which is still displaying the classes tab item with AppController selected. Now we need to create an instance of this class, by selecting Instantiate App Controller from the classes menu.

When you instantiate the controller, it will appear in the Instances tab item.

Now we need to connect everything together. Control Drag from the AppController instance to input field on the application window and click connect in the inspector. Do the same for the result field. Now control drag from the button on the window to the AppController and connect the button to the countLetters: action.

Finally, we will add the necessary files for the AppController class to out project. Go to the classes tab and select the AppController class. Now go to the Classes menu and select Create Files for AppController. This should bring up a dialog to add the AppController.h and AppController.m to the LetterCounter target. Click the choose button to add the files.

Now save the nib file. We've done all we can here.

Now go back to Xcode and edit the AppController.m file. There should already be a countLetters: method ready to be completed. Edit the method like this...

- (IBAction)countLetters:(id)sender
{
NSString *entry = [inputTextField stringValue];

int letterCount = [entry length];

NSString *result =
[NSString stringWithFormat:@"%@ has %d letters."
,entry, letterCount]

[resultTextField setStringValue:result];
}


That should do it. Save the AppController.m file, build it and run it.

If things don't work, be sure to go and check your connections in the nib file, and double check the code for the method.

Happy Cocoa Travels!

Comments: Post a Comment


<< Home

This page is powered by Blogger. Isn't yours?