Online Book Reader

Home Category

Cocoa Programming for Mac OS X - Aaron Hillegass [38]

By Root 792 0
startSpeakingString:string];

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

[_stopButton setEnabled:YES];

[_speakButton setEnabled:NO];

}

In speechSynthesizer:didFinishSpeaking:, reset the buttons to their initial states:

- (void)speechSynthesizer:(NSSpeechSynthesizer *)sender

didFinishSpeaking:(BOOL)finishedSpeaking

{

NSLog(@"finishedSpeaking = %d", finishedSpeaking);

[_stopButton setEnabled:NO];

[_speakButton setEnabled:YES];

}

Build and run the application. You should see that the Stop button is enabled only when the synthesizer is generating speech. The Speak button should be enabled only when the synthesizer is silent.

The NSTableView and Its dataSource


Next, you will add a table view that will enable the user to change the voice, as shown in Figure 6.4.

Figure 6.4. Completed Application

A table view is used for displaying columns of data. An NSTableView has a helper object called a dataSource, as shown in Figure 6.5. The table view expects its data source to have some methods. We say, “The data source must conform to the NSTableDataSource informal protocol.” This is a fancy way of saying that it must implement these two methods:

- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView;

The dataSource will reply with the number of rows that will be displayed.

Figure 6.5. NSTableView’s dataSource

- (id)tableView:(NSTableView *)aTableView

objectValueForTableColumn:(NSTableColumn *)aTableColumn

row:(NSInteger)rowIndex;

The dataSource will reply with the object that should be displayed in the row rowIndex of the column aTableColumn.

If you have editable cells in your table view and are using a cell-based table view (as we are in this exercise), you will need to implement one more method:

- (void)tableView:(NSTableView *)aTableView

setObjectValue:(id)anObject

forTableColumn:(NSTableColumn *)aTableColumn

row:(NSInteger)rowIndex;

The dataSource takes the input that the user put into row rowIndex of aTableColumn. You do not have to implement this method if your table view is not editable.

Note that you are taking a very passive position in getting data to appear. Your data source will wait until the table view asks for the data. When they first work with NSTableView (or NSBrowser or NSOutlineView, which work in a very similar manner), most programmers want to boss the table view around and tell it, “You will display 7 in the third row in the fifth column.” It doesn’t work that way. When it is ready to display the third row and the fifth column, the table view will ask its dataSource for the object to display. Your class is its servant.

How, then, will you get the table view to fetch updated information? You will tell the table view to reloadData. It will then reload all the cells that the user can see.

SpeakLineAppDelegate Interface File


You are going to make your instance of SpeakLineAppDelegate become the dataSource of the table view. This involves two steps: implementing the two methods listed earlier and setting the table view’s dataSource outlet to the instance of SpeakLineAppDelegate. Figure 6.6 provides a diagram of where you are going.

Figure 6.6. Object Diagram

First, add the declaration of a new instance variable to SpeakLineAppDelegate.h:

#import

@interface SpeakLineAppDelegate : NSObject

{

NSArray *_voices;

NSSpeechSynthesizer *_speechSynth;

}

Save the file. In SpeakLineAppDelegate.m, change the init method to initialize _voices:

- (id)init

{

self = [super init];

if (self) {

// Create a new instance of NSSpeechSynthesizer

// with the default voice.

_speechSynth = [[NSSpeechSynthesizer alloc]

initWithVoice:nil];

[_speechSynth setDelegate:self];

_voices = [NSSpeechSynthesizer availableVoices];

}

return self;

}

Lay Out the User Interface


Open MainMenu.xib and select the window icon in the dock to show the window. You will edit the window to look like Figure 6.7.

Figure 6.7. Completed Interface

Return Main Page Previous Page Next Page

®Online Book Reader