吉吉于

free

Cocoa学习笔记(2):delegate

接着用上一节朗读的例子来演示。

如果你想让程序在朗读的时候使start按钮变灰,而stop按钮可以点击,那就可以通过设置speechSynth的delegate来实现效果。

speechSynth将委托(delegate)给自己,即AppController。

调用speechSynth的委托通过AppController来进行其他操作,比如设置按钮变灰。

不知道我说没说明白,我是从C#那里的委托来理解Obj-C的委托的,大致差不多吧,不对的地方帮忙提出来哦。

效果如下:

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

对于NSSpeechSynthesizer的Delegate函数有:

– speechSynthesizer:willSpeakWord:ofString:
– speechSynthesizer:willSpeakPhoneme:
– speechSynthesizer:didEncounterErrorAtIndex:ofString:message:
– speechSynthesizer:didEncounterSyncMessage:
– speechSynthesizer:didFinishSpeaking:

这些可以在Document查询。

关系图:

//
//  AppController.m
//  SpeakLine
//
//  Created by 哲   于 on 12-8-18.
//  Copyright (c) 2012年 哈尔滨理工大学. All rights reserved.
//

#import "AppController.h"

@implementation AppController

- (id)init{

    NSLog(@"init");
    speechSynth=[[NSSpeechSynthesizer alloc] initWithVoice:nil];
    [speechSynth setDelegate:self];
    return self;
}

- (IBAction)sayIt:(id)sender{
    NSString *string=[textField stringValue];
    if([string length]==0){
        NSLog(@"The textField %@ is of zero-length",textField);
        return;
    }
    [speechSynth startSpeakingString:string];
    NSLog(@"Have started to say:%@",string);
}

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

- (void)speechSynthesizer:(NSSpeechSynthesizer *)sender didFinishSpeaking:(BOOL)complete{
    NSLog(@"complete = %d", complete);
    [startButton setEnabled:YES];
    [stopButton setEnabled:NO];
}

- (void)speechSynthesizer:(NSSpeechSynthesizer *)sender willSpeakPhoneme:(short)phonemeOpcode{
    [startButton setEnabled:NO];
    [stopButton setEnabled:YES];
}

@end

 下载源码

转载请注明:于哲的博客 » Cocoa学习笔记(2):delegate