SetDelegate Description

Hi, I am new to iphone development, can someone explain to me why setDelegate is used, where should we use it.

[request setDelegate:sender]; 

early.

+4
source share
2 answers

Delegates are just a design template; no special syntax or language support.

A delegate is simply an object to which another object sends messages when certain things happen, so that the delegate can process application-specific details for which the original object was not created. This is a way to customize behavior without a subclass.

+6
source

Some classes, such as NSSpeechSynthesizer, include delegate support. Unlike the protocol, a refusal to provide a delegate method does not cause an error: the class always provides a method, but instead calls yours if it exists.

For example, NSSpeechSynthesizer has a method

 -(void) speechSynthesizer:(NSSpeechSynthesizer*)sender didFinishSpeaking:(BOOL)complete; 

If you provide an identically declared method, in the Fred class it will be called instead of your own synthesizer method, if you previously did this in this class,

 speech = [[NSSpeechSynthesizer alloc] initWithVoice:@"com.apple.speech.synthesis.voice.Albert"]; [speech setDelegate:self]; 

This will work, although the compiler will warn you if you have not declared yourself as a delegate

 @interface Fred : NSObject <NSSpeechSynthesizerDelegate>, in that { 

.,.

(This example is adapted from Cocoa Programming ... by Hillegass).

+2
source

Source: https://habr.com/ru/post/1309300/


All Articles