What is the meaning of the delegate, for example Objective-C?


Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/public_html/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
12 views
Can someone in simple language to explain? Watched Stanford courses, like everything is clear, but with delegates like that did not happen.
by | 12 views

4 Answers

0 like 0 dislike
Think of a delegate as an ordinary object that can perform some of the functions. For example, take the NSTableView delegate. You want to render a table cell as something on its own. My NSTableView delegate will send the message that he will now draw the cell, and the delegate already decides what to do with it (draw on the, do not touch at all, etc.). This is, roughly speaking, the method of obtaining and providing information about which NSTableView doesn't know anything.
Or an example of creating your own delegates. Imagine you have a class that performs a certain function. For some tasks he needs information from another class, which is now not known absolutely nothing, except that it exists. We can create a design view:
@interface Class1 {
id delegate;
}
\r
— (id)delegate;
— (void)setDelegate:(id)newDelegate;
\r
@implementation Class1
\r
— (id)delegate {
return delegate;
}
\r
— (void)setDelegate:(id)newDelegate {
delegate = newDelegate;
}
\r
As you can see, our delegate is just a pointer to any object. Well, provided the getter and setter. In order that the delegate has performed some action for us, somewhere within our Class1 we send a message of the form [delegate doSomeWork];
The object, which we have appointed the delegate for this class in turn receives the message and take some action.
In principle, and all. Simple enough.
by
0 like 0 dislike
Good explanation and directly to delegates in Objective-C:
\rwww.youtube.com/watch?v=ZhO4FmajxgM
Well, the paragraph in Apple's documentation also describes well the work of the delegates: developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CocoaFundamentals/CommunicatingWithObjects/CommunicateWithObjects.html#//apple_ref/doc/uid/TP40002974-CH7-SW18
by
0 like 0 dislike
Delegation pursues a simple goal — to share the responsibility between objects, so that each went about their business while keeping objects loosely coupled. So you can send messages to the delegate, not knowing what exactly is the object. And the delegate, thus, can perform different actions depending on its implementation. So here we have one of the applications of polymorphism.
\r
That is, roughly speaking, a delegate object tells the object to delegate WHAT to do, but he doesn't care HOW it is done.
\r
Plus, delegation can sometimes be a more convenient alternative to inheritance — instead of having to produce arario classes you define the required interface for delegates and use them.
by
0 like 0 dislike
Delegate — a way of organizing callbacks/message processing. Immediately obvious which messages can be processed, the code collected in one place, etc. for Example, in Win32 instead of the delegate written your message handler, which filters are needed, and for the rest causing the procedure by default; to Cocoa procedure the default setting is always, but if you have a delegate, it sends some messages to him.
by

Related questions

0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
110,608 questions
257,186 answers
0 comments
28,070 users