我无法从可可编程书中得到一个例子来工作(我认为部分问题是 XCode 已经过时了)。除了在 IB 中建立适当的连接之外,它没有提到我必须做任何特别的事情来接收来自 NSTableView 的通知。该应用程序是一个带有用于用户输入的 NSTextField 的程序,当单击按钮时,NSSpeechSynthesizer 的实例将说出输入的行。我应该能够通过在表格视图上选择一行来切换说话的声音。我可以点击,该应用程序在技术上有效,但我无法通过点击 GUI 上 NSTableView 上的一行来更改默认语音:
appdelegate.h:
#import <Cocoa/Cocoa.h>
@interface AppDelegate : NSObject <NSApplicationDelegate,NSSpeechSynthesizerDelegate,NSTableViewDelegate>
{
NSArray *_voices;
NSSpeechSynthesizer *_speechSynth;
}
@property (assign) IBOutlet NSWindow *window;
@property (weak) IBOutlet NSTextField *textField;
@property (assign) IBOutlet NSTextField *text;
@property (assign) IBOutlet NSButton *_speakButton;
@property (assign) IBOutlet NSButton *_stopButton;
@property (assign) IBOutlet NSTableView *table;
-(IBAction)sayIt:(id)sender;
-(IBAction)stopIt:(id)sender;
@end
appdelegate.m:
#import "AppDelegate.h"
@implementation AppDelegate
@synthesize window = _window;
@synthesize textField = _textField;
@synthesize text = _text;
@synthesize _speakButton;
@synthesize _stopButton;
@synthesize table;
-(void)speechSynthesizer:(NSSpeechSynthesizer *)sender didFinishSpeaking:(BOOL)finishedSpeaking
{
NSLog(@"finishedSpeaking=%d",finishedSpeaking);
[_speakButton setEnabled:YES];
[_stopButton setEnabled:NO];
[table setEnabled:YES];
}
-(id)init
{
NSLog(@"init called");
self = [super init];
if (self)
{
NSLog(@"init");
_speechSynth = [[NSSpeechSynthesizer alloc] initWithVoice:nil];
[_speechSynth setDelegate:self];
[_text setStringValue:@""];
_voices = [NSSpeechSynthesizer availableVoices];
}
//I added this line myself as I was trying to figure it out
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tableViewSelectionDidChange:) name:@"MyNotification" object:table];
return self;
}
-(IBAction)sayIt:(id)sender
{
NSString *string = [_textField stringValue];
if([string length]==0)
{
NSLog(@"string from %@ is of zero-length",_textField);
return;
}
[_speechSynth startSpeakingString:string];
[_text setStringValue:[_speechSynth voice]];
...
}
-(IBAction)stopIt:(id)sender
{
NSLog(@"stopping");
...
}
-(NSInteger)numberOfRowsInTableView:(NSTableView *)tv
{
return (NSInteger)[_voices count];
}
-(id)tableView:(NSTableView *)tv
objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
NSString *v = [_voices objectAtIndex:row];
NSDictionary *dict =[NSSpeechSynthesizer attributesForVoice:v];
return [dict objectForKey:NSVoiceName];
}
-(void)tableViewSelectionDidChange:(NSNotification *)notification
{
NSInteger row = [table selectedRow];
if(row==-1) return;
NSString *selectedVoice = [_voices objectAtIndex:row];
[_speechSynth setVoice:selectedVoice];
NSLog(@"new voice = %@",selectedVoice);
}
-(void)awakeFromNib
{
NSLog(@"awakeFromNib");
NSString *defaultVoice = [NSSpeechSynthesizer defaultVoice];
NSInteger defaultRow = [_voices indexOfObject:defaultVoice];
NSIndexSet *indices = [NSIndexSet indexSetWithIndex:defaultRow];
[table selectRowIndexes:indices byExtendingSelection:NO];
[table scrollRowToVisible:defaultRow];
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
NSLog(@"Appdidfinishlaunching called");
}
@end
我猜我走错了这条路。我只需要有人指出我应该如何工作的正确方向。