我不明白如何在Cocoa中使用委托,但我知道它们是什么

lam*_*ade 2 cocoa objective-c

像许多人一样,我对Objective-C和Cocoa编程很感兴趣.我从概念上知道代理是什么,但我不明白如何使用它们或何时使用它们.这是一些示例代码:

#import "AppControler.h"


@implementation AppControler

-(id)init
{
    [super init];
    NSLog(@"init");

    speechSynth = [[NSSpeechSynthesizer alloc] initWithVoice:nil];
    //
    [speechSynth setDelegate:self];
    voiceList = [[speechSynth availableVoices] retain];

    return self;
}
Run Code Online (Sandbox Code Playgroud)

我将AppControler设置为speechSynthasizer的委托.这意味着speechSynthasizer告诉了AppControler该怎么做.但我不明白这一行: [speechSynth setDelegate:self];

Ark*_*kku 5

self是当前对象,因此[speechSynth setDelegate:self]将对象的委托设置为speechSynth当前对象,即您的AppControler(原文如此)实例.

编辑:除了显示的代码之外,您还AppControler应该为要委派给它的消息实现NSSpeechSynthesizerDelegate协议.