TweetFollow Us on Twitter

Creating a Cocoa AppController Class

Volume Number: 21 (2005)
Issue Number: 1
Column Tag: Programming

Getting Started

by Dave Mark

Creating a Cocoa AppController Class

In our last Cocoa column, we downloaded the latest and greatest version of Xcode. We created a Foundation Tool, which is an Objective-C program with a console-based interface.

This month, we're going to build a Cocoa app with an interface we designed using Interface Builder. The app will use Cocoa's NSSpeechSynthesizer class to speak a line of text. We'll add a pushbutton to start the speech and another to halt it, even in mid-sentence. The example comes from Chapter 4 of Aaron Hillegass' book, Cocoa Programming for Mac OS X. We'll start by taking a look at a class diagramming approach Aaron uses throughout the book.

A Diagram Speaks a Thousand Words

Before we actually start the process of building our project, take a look at the object diagram shown in Figure 1. This diagramming convention was developed by Aaron Hillegas and I find it works quite well at describing the interrelationships between the classes, objects, methods, and instance variables that come together to make your program work.

The class at the heart of this example is the AppController class. Note that this class features two methods: sayIt: and stopIt:. In Hillegas' drawings, each box represents a class and each arrow connecting two boxes represents the control-dragging connection you create in Interface Builder. For example, in Figure 1, note that 4 of the 5 classes are Cocoa classes (they start with NS). All of our code will be built into a new class that we create called AppController. We'll create two instances of NSButton, one labeled Say it and one labeled Stop. Each of the buttons will target one of the two AppController methods. When we build the project in Interface Builder, we'll create an instance of AppController, then control-drag from each button to the AppController instance and double-click on the method we want called to finish the connection.

We'll also add instance variables to AppController to keep track of the NSTextField (so we can retrieve the text to say it) and the NSSpeechSynthesizer (so we can send it the text to start speaking and send it a stop message to halt the speaking).


Figure 1. An object diagram for the first incarnation of our SpeakLine program.

Create the SpeakLine Project

Launch Xcode and create a new project using the Cocoa Application template. Name the project SpeakLine.

Editing the .nib File

In your SpeakLine project file, in the Groups & Files pane, find the file MainMenu.nib and double-click it to launch Interface Builder. You can find the file in the NIB Files group as well as under the SpeakLine group, in the Resources subgroup.

Once Interface Builder launches, click on the third icon from the left in the palette window, then drag an NSTextField from the palette onto the main window. As you can see in Figure 2, the NSTextField is in the upper-left corner of the set of text items.


Figure 2. Dragging out an NSTextField.

Drag the NSTextField so it is almost as wide as the window (so the dashed blue line appears when you get about a scrollbar's width from the right side of the window). Double-click on the text field and change its text to read Peter Piper picked a peck of pickled peppers or, if you are by yourself, perhaps something a bit more spicy.

Next, click on the second icon from the left at the top of the palette window to show the control palette items. Drag two buttons onto the window, below the NSTextField, with the proper spacing between them and the right side of the window. Label the right button Say It and the left button Stop (double-click on a button to edit its label).

Finally, resize the window itself, making it as short as possible. Figure 3 shows my Interface Builder session. In this picture, I am dragging the Stop button into place. You can see the dashed blue lines showing that the two buttons are aligned with each other and that the Stop button is the correct distance from the text field above it and the Say It button to its right.


Figure 3. Use the blue dashed lines to line up your buttons and NSTextField.

Create the AppController Class

Now that your interface is laid out, it's time to create the new AppController class.

Click on the MainMenu.nib window and click on the Classes tab. Scroll all the way to the left and click on the NSObject class. With NSObject highlighted, select Subclass NSObject from the Classes menu. Name the new subclass AppController (see Figure 4).


Figure 4. Click on the NSObject class and select Subclass NSObject from the Classes menu.

Now you'll add two actions (one for each button) and an outlet (an instance variable that points to the text field) to AppController. Open the Info window by selecting Show Info from the Tools menu. Click on the AppController class in the classes tab in the MainMenu.nib window, then click on the Info window and select Attributes from the popup near the top of the Info window.

Click on the Actions tab, then click on the Add button at the bottom right of the Info window. When the new action appears, name it sayIt:, then click Add and name the second action stopIt: (see Figure 5).


Figure 5. The Info window, showing the AppController class attributes.

Next, click on the Outlet tab and click Add to add an outlet named textField to AppController. Click in the Type column and select NSTextField to set the textField type to NSTextField instead of the generic id.

If you look back at Figure 1, you'll see that we've addressed 3 of the 4 arrows in the object diagram. We'll add the missing outlet, speechSynth, in code in just a minute.

Be sure that the AppController class is selected in the Classes tab and select Create Files for AppController from the Classes menu. This will generate two source files (AppController.m and AppController.h) in your Xcode project which we'll edit in a bit.

Next, create an instance of the AppController class by selecting Instantiate AppController from the Classes menu. Interface Builder will switch the MainMenu.nib window to the Instances tab and a new, blue cube will appear with the name AppController.

As you can see in Figure 6, the AppController instance is represented by a blue cube. The tiny exclamation point in a circle to the lower right of the blue cube tells you that there is at least one unconnected outlet. Let's take care of that now.


Figure 6. The new instance of AppController with an unconnected outlet.

Making Connections

Before you start making your connections, take a quick peek back at Figure 1. There are four connections that need to be made. Three of them will be made by control-dragging. The fourth (speechSynth) will be made in code.

First, we'll connect AppController's textField outlet so it points to the NSTextField in the main window. Make sure the Info window is open before you start your drag.

Control-drag from the AppController blue cube to the text field in the main window. When you release the mouse button, the Info window should display its Connections pane and list the textField outlet. Either double-click on the textField line or make sure it is selected and click the Connect button in the lower-right corner of the Info window (Figure 7).


Figure 7. Click the Connect button to connect the AppController to the textField.

Next, we'll connect the two buttons to their respective AppController methods. Control-drag from the Say It button to the AppController cube then, in the Info window, connect to the sayIt: method.

Now control-drag from the Stop button to the AppController cube and connect to the stopIt: method.

NSWindow's initialFirstResponder

The last bit of Interface Builder work we'll do is to set the NSWindow initialFirstResponder outlet to point to the text field. This tells the window that you want the text field to be active when the window appears so you don't have to click in the text field to start typing. To get a feel for this, try running the program with the initialFirstResponder connected and then with it disconnected to see what happens.

Control-drag from the Window icon (to the left of the blue AppController cube) to the text field. In the Info window, click on the initialFirstResponder outlet and click the Connect button.

Now let's type in the code!

Enter the AppController Code

Head back over to Xcode and edit the AppController.h file. We'll add the declaration of speechSynth:


#import <Cocoa/Cocoa.h>


@interface AppController : NSObject 
{
	IBOutlet NSTextField *textField;
	NSSpeechSynthesizer *speechSynth;
} 

- (IBAction)sayIt:(id)sender;
- (IBAction)stopIt:(id)sender; 
@end

Next, edit AppController.m to look like this:

#import "AppController.h"
@implementation AppController

- (id)init 
{  
	[super init];
 	NSLog( @"init" );
 	speechSynth = [[NSSpeechSynthesizer alloc] initWithVoice:nil];
	return self; 
 }


- (IBAction)sayIt:(id)sender 
{  
	NSString *string = [textField stringValue];
 if ( [string length] == 0) {   
 	return;  
 }

 [speechSynth startSpeakingString:  string];

 	NSLog( @"Have started to say: %@", string ); 
 }

- (IBAction)stopIt:(id)sender 
{
	NSLog( @"stopping" );
	[speechSynth stopSpeaking]; 
}

- (void)dealloc 
{
	NSLog( @"dealloc" );
	[speechSynth release];
	[super dealloc];
}
@end 

Build and run the application. Notice that you can click the Stop button to stop the speaking, even in the middle.

Take a look through the code. Most of it should make sense, especially if you've been following along with my previous Cocoa columns.

The init: method calls the superclasses' init drops a message to the console, creates an instance of the NSSpeechSynthesizer, then returns a pointer to itself.

sayIt: sends a stringValue message to textField to retrieve the text, then, if there's at least one character in the field, send it via a startSpeakingString message to speechSynth. The string is sent to the console as well, just to help you follow along.

stopIt: sends a message to the console, then sends a stopSpeaking message to speechSynth.

dealloc: is called when the AppController object is released. You'll likely never see the console message, since the AppController object was created automatically and is never sent a release message. When it is loaded from the .nib file, the AppController instance has a ref count of one. Not a big deal, but worth noting.

Till Next Month...

One thing that Aaron does in his book is add a color well to the program so the user can choose their own text color. See if you can do this on your own. You'll want to take advantage of the NSColorWell class.

Be sure to check out http://www.spiderworks.com and I'll see you next month...


Dave Mark is a long-time Mac developer and author and has written a number of books on Macintosh development, including Learn C on the Macintosh, Learn C++ on the Macintosh, and The Macintosh Programming Primer series. Dave's been busy lately cooking up his next concoction. Want a peek? http://www.spiderworks.com.

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Whitethorn Games combines two completely...
If you have ever gone fishing then you know that it is a lesson in patience, sitting around waiting for a bite that may never come. Well, that's because you have been doing it wrong, since as Whitehorn Games now demonstrates in new release Skate... | Read more »
Call of Duty Warzone is a Waiting Simula...
It's always fun when a splashy multiplayer game comes to mobile because they are few and far between, so I was excited to see the notification about Call of Duty: Warzone Mobile (finally) launching last week and wanted to try it out. As someone who... | Read more »
Albion Online introduces some massive ne...
Sandbox Interactive has announced an upcoming update to its flagship MMORPG Albion Online, containing massive updates to its existing guild Vs guild systems. Someone clearly rewatched the Helms Deep battle in Lord of the Rings and spent the next... | Read more »
Chucklefish announces launch date of the...
Chucklefish, the indie London-based team we probably all know from developing Terraria or their stint publishing Stardew Valley, has revealed the mobile release date for roguelike deck-builder Wildfrost. Developed by Gaziter and Deadpan Games, the... | Read more »
Netmarble opens pre-registration for act...
It has been close to three years since Netmarble announced they would be adapting the smash series Solo Leveling into a video game, and at last, they have announced the opening of pre-orders for Solo Leveling: Arise. [Read more] | Read more »
PUBG Mobile celebrates sixth anniversary...
For the past six years, PUBG Mobile has been one of the most popular shooters you can play in the palm of your hand, and Krafton is celebrating this milestone and many years of ups by teaming up with hit music man JVKE to create a special song for... | Read more »
ASTRA: Knights of Veda refuse to pump th...
In perhaps the most recent example of being incredibly eager, ASTRA: Knights of Veda has dropped its second collaboration with South Korean boyband Seventeen, named so as it consists of exactly thirteen members and a video collaboration with Lee... | Read more »
Collect all your cats and caterpillars a...
If you are growing tired of trying to build a town with your phone by using it as a tiny, ineffectual shover then fear no longer, as Independent Arts Software has announced the upcoming release of Construction Simulator 4, from the critically... | Read more »
Backbone complete its lineup of 2nd Gene...
With all the ports of big AAA games that have been coming to mobile, it is becoming more convenient than ever to own a good controller, and to help with this Backbone has announced the completion of their 2nd generation product lineup with their... | Read more »
Zenless Zone Zero opens entries for its...
miHoYo, aka HoYoverse, has become such a big name in mobile gaming that it's hard to believe that arguably their flagship title, Genshin Impact, is only three and a half years old. Now, they continue the road to the next title in their world, with... | Read more »

Price Scanner via MacPrices.net

B&H has Apple’s 13-inch M2 MacBook Airs o...
B&H Photo has 13″ MacBook Airs with M2 CPUs and 256GB of storage in stock and on sale for up to $150 off Apple’s new MSRP, starting at only $849. Free 1-2 day delivery is available to most US... Read more
M2 Mac minis on sale for $100-$200 off MSRP,...
B&H Photo has Apple’s M2-powered Mac minis back in stock and on sale today for $100-$200 off MSRP. Free 1-2 day shipping is available for most US addresses: – Mac mini M2/256GB SSD: $499, save $... Read more
Mac Studios with M2 Max and M2 Ultra CPUs on...
B&H Photo has standard-configuration Mac Studios with Apple’s M2 Max & Ultra CPUs in stock today and on Easter sale for $200 off MSRP. Their prices are the lowest available for these models... Read more
Deal Alert! B&H Photo has Apple’s 14-inch...
B&H Photo has new Gray and Black 14″ M3, M3 Pro, and M3 Max MacBook Pros on sale for $200-$300 off MSRP, starting at only $1399. B&H offers free 1-2 day delivery to most US addresses: – 14″ 8... Read more
Department Of Justice Sets Sights On Apple In...
NEWS – The ball has finally dropped on the big Apple. The ball (metaphorically speaking) — an antitrust lawsuit filed in the U.S. on March 21 by the Department of Justice (DOJ) — came down following... Read more
New 13-inch M3 MacBook Air on sale for $999,...
Amazon has Apple’s new 13″ M3 MacBook Air on sale for $100 off MSRP for the first time, now just $999 shipped. Shipping is free: – 13″ MacBook Air (8GB RAM/256GB SSD/Space Gray): $999 $100 off MSRP... Read more
Amazon has Apple’s 9th-generation WiFi iPads...
Amazon has Apple’s 9th generation 10.2″ WiFi iPads on sale for $80-$100 off MSRP, starting only $249. Their prices are the lowest available for new iPads anywhere: – 10″ 64GB WiFi iPad (Space Gray or... Read more
Discounted 14-inch M3 MacBook Pros with 16GB...
Apple retailer Expercom has 14″ MacBook Pros with M3 CPUs and 16GB of standard memory discounted by up to $120 off Apple’s MSRP: – 14″ M3 MacBook Pro (16GB RAM/256GB SSD): $1691.06 $108 off MSRP – 14... Read more
Clearance 15-inch M2 MacBook Airs on sale for...
B&H Photo has Apple’s 15″ MacBook Airs with M2 CPUs (8GB RAM/256GB SSD) in stock today and on clearance sale for $999 in all four colors. Free 1-2 delivery is available to most US addresses.... Read more
Clearance 13-inch M1 MacBook Airs drop to onl...
B&H has Apple’s base 13″ M1 MacBook Air (Space Gray, Silver, & Gold) in stock and on clearance sale today for $300 off MSRP, only $699. Free 1-2 day shipping is available to most addresses in... Read more

Jobs Board

Medical Assistant - Surgical Oncology- *Apple...
Medical Assistant - Surgical Oncology- Apple Hill Location: WellSpan Medical Group, York, PA Schedule: Full Time Sign-On Bonus Eligible Remote/Hybrid Regular Apply Read more
Omnichannel Associate - *Apple* Blossom Mal...
Omnichannel Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Cashier - *Apple* Blossom Mall - JCPenney (...
Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Mall Read more
Operations Associate - *Apple* Blossom Mall...
Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Business Analyst | *Apple* Pay - Banco Popu...
Business Analyst | Apple PayApply now " Apply now + Apply Now + Start applying with LinkedIn Start + Please wait Date:Mar 19, 2024 Location: San Juan-Cupey, PR Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.