Online Book Reader

Home Category

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

By Root 865 0

#import

@interface SpeakLineAppDelegate : NSObject

{

NSSpeechSynthesizer *_speechSynth;

}

@property (assign) IBOutlet NSWindow *window;

@property (weak) IBOutlet NSTextField *textField;

- (IBAction)sayIt:(id)sender;

- (IBAction)stopIt:(id)sender;

@end

Note that because we are not setting the speechSynth variable in the XIB file, we don’t flag it as IBOutlet.

Open the SpeakLineAppDelegate.m file. This is where you will make the methods do something:

#import "SpeakLineAppDelegate.h"

@implementation SpeakLineAppDelegate

@synthesize window = _window;

@synthesize textField = _textField;

- (id)init

{

self = [super init];

if (self) {

// Logs can help the beginner understand what

// is happening and hunt down bugs.

NSLog(@"init");

// Create a new instance of NSSpeechSynthesizer

// with the default voice.

_speechSynth = [[NSSpeechSynthesizer alloc]

initWithVoice:nil];

}

return self;

}

- (IBAction)sayIt:(id)sender

{

NSString *string = [_textField stringValue];

// Is the string zero-length?

if ([string length] == 0) {

NSLog(@"string from %@ is of zero-length", _textField);

return;

}

[_speechSynth startSpeakingString:string];

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

}

- (IBAction)stopIt:(id)sender

{

NSLog(@"stopping");

[_speechSynth stopSpeaking];

}

@end

Your application is done. Build it and run it. You should be able to start the recitation of the text in the text field and stop it in mid-sentence.

Final note: A menu item (an instance of NSMenuItem) also has a target and an action. Everything we’ve talked about in this chapter applies to menu items.

For the More Curious: Setting the Target Programmatically


Note that the action of a control is a selector. NSControl includes the following method:

- (void)setAction:(SEL)aSelector

But how would you get a selector? The Objective-C compiler directive @selector will tell the compiler to look up the selector for you. For example, to set the action of a button to the method drawMickey:, you could do the following:

SEL mySelector;

mySelector = @selector(drawMickey:);

[myButton setAction:mySelector];

At compile time, @selector(drawMickey:) will be replaced by the selector for drawMickey:.

If you needed to find a selector for an NSString at runtime, you could use the function NSSelectorFromString():

SEL mySelector;

mySelector = NSSelectorFromString(@"drawMickey:");

[myButton setTarget:someObjectWithADrawMickeyMethod];

[myButton setAction:mySelector];

Challenge


This exercise is an important challenge you should do before moving on. Although it is easy to follow the instructions, you will eventually want to create your own applications. Here is where you can start to develop some independence. Feel free to refer back to the earlier examples for guidance.

Create another application that will present the user with the window shown in Figure 5.13. This application can have only one window open, so it is not a document-based application.

Figure 5.13. Before Input

When the user types in a string and clicks the button, change the message text to display the input string and the number of characters it has (Figure 5.14).

Figure 5.14. After Input

It is important to know how to use the Cocoa classes in your application. For this exercise, you should recognize that the NSTextField class has the following methods:

- (NSString *)stringValue;

- (void)setStringValue:(NSString *)aString;

You will also find it useful to know about the following methods of the class NSString:

- (NSUInteger)length;

+ (NSString *)stringWithFormat:(NSString *),...;

You will create a controller object with two outlets and one action. (This is hard, and you are not stupid. Good luck!)

Debugging Hints


Now that you are writing code, not just copying it from the book, you are ready for some debugging hints.

Always watch the console. If a Cocoa object throws an exception, it will be logged to the console and the event

Return Main Page Previous Page Next Page

®Online Book Reader