Re: Delegates
Re: Delegates
- Subject: Re: Delegates
- From: Michael Vannorsdel <email@hidden>
- Date: Mon, 19 May 2008 10:52:09 -0600
Delegates act like observers. They register with another object
saying "hey, if you do something important I want to know about it and
have a chance to act on it too". Each class that allows delegates
have a list of delegate methods. These are the methods the class is
willing to notify the delegate with when some even happens. For
instance NSApplication has a delegate method
applicationShouldTerminate:. If an object registers as a delegate to
NSApp and NSApp wants to terminate, it will ask its delegate if it's
ok to do so by calling applicationShouldTerminate: on the delegate.
The delegate can reply with "not yet, I have stuff I need to finish
first" and NSApp will cancel the quit. Or it can respond with "that's
fine with me, please continue". A delegate can be any object of any
class and can be the delegate for any number of other important
objects. It can also choose to only implement the delegate methods it
cares about. If in the example the delegate for NSApp doesn't
implement applicationShouldTerminate:, NSApp won't bother asking as
it's clear the delegate doesn't care about termination.
Delegate methods are mostly like notifications, except there can be a
chance for the notified object to intervene and partake in the event.
NSNotifications are sent as-is; it's already happening so deal with it.
On May 19, 2008, at 10:22 AM, john darnell wrote:
I've been trying to get my head wrapped around the concept of
"delegates" and I thought I would run it by the list to see if I am
approaching the correct idea behind a delegate.
As far as I can tell, it is kind of like a virtual function (virtual
because I, the programmer, am expected to flesh it out) that resembles
an event attached to a given class. It has specific times when it is
called and at that time it may perform just about any duty that the
programmer can dream of, or she can stick to something that relates to
the object calling it. In almost every description of a delegate
that I
perused, I found the words "when" or "before," which is why I
related a
delegate to an event.
Am I getting close?
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
References: | |
| >Delegates (From: "john darnell" <email@hidden>) |