M
M
Meliborn2011-01-30 00:48:28
Objective-C
Meliborn, 2011-01-30 00:48:28

What is the meaning of delegate in Objective-C example?

Can someone explain in simple terms? I watched the Stanford courses, everything seems to be clear, but somehow it didn’t work out with the delegates.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
P
Paulskit, 2011-01-30
@Paulskit

Think of a delegate as just an object that can perform some function. For example, take the NSTableView delegate. You want to draw a table cell in your own way. NSTableView will send a message to its delegate that it will now draw this cell and the delegate decides what to do with it (draw it in its own way, do not touch it at all, etc.). This is, roughly speaking, a way to receive and provide information about which NSTableView does not know anything at all.
Or an example of creating your own delegates. Imagine that you have your own class that performs some function. To perform some tasks, he needs information from another class, about which absolutely nothing is known at the moment, except that it exists. Then a construction of the form is created:
@interface Class1 {
delegate id;
}
- (id)delegate;
- (void)setDelegate:(id)newDelegate;
@implementation Class1
- (id)delegate {
return delegate;
}
- (void)setDelegate:(id)newDelegate {
delegate = newDelegate;
}
As you can see from the example, our delegate is just a pointer to some object. Well, getter and setter are provided. In order for the delegate to perform some action for us, somewhere inside our Class1 we will send a message like [delegate doSomeWork];
The object that we have appointed as a delegate for this class, in turn, will receive this message and begin to perform some action.
Basically, that's all. Simple enough.

P
petrovi4, 2011-01-30
@petrovi4

A good explanation of delegates directly and specifically in Objective-C:
www.youtube.com/watch?v=ZhO4FmajxgM
Well, a paragraph in the Apple documentation also describes the work of delegates well: developer.apple.com/library/mac/#documentation/Cocoa/Conceptual /CocoaFundamentals/CommunicatingWithObjects/CommunicateWithObjects.html#//apple_ref/doc/uid/TP40002974-CH7-SW18

B
bsboris, 2011-01-30
@bsboris

Delegation has the simple goal of sharing responsibility between objects so that everyone does their own thing while keeping the objects loosely coupled. This way you can send messages to a delegate without knowing which object it is. And the delegate itself, at the same time, can perform different actions depending on its implementation. So here we have one of the uses of polymorphism.
That is, roughly speaking, the delegating object tells the delegate object WHAT to do, but it does not care HOW exactly it will be done.
Plus, delegation can sometimes be a more convenient alternative to inheritance - instead of producing a hierarchy of classes, you define the necessary interface for delegates and use them.

M
MikhailEdoshin, 2011-02-05
@MikhailEdoshin

A delegate is a way to organize callbacks/message processing. You can immediately see which messages can be processed, the code is collected in one place, etc. For example, in Win32, instead of a delegate, you write your own message handler that filters the necessary ones, and calls the default procedure for the rest; in Cocoa, the default procedure is always executed, but if there is a delegate, it passes some messages to it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question