Re: How does Cocoa implement delegate/callback?
Re: How does Cocoa implement delegate/callback?
- Subject: Re: How does Cocoa implement delegate/callback?
- From: matt neuburg <email@hidden>
- Date: Fri, 15 Nov 2002 10:08:01 -0800
On Thu, 14 Nov 2002 12:41:29 -0500, Bill Cheeseman <email@hidden> said:
>
There are many Cocoa methods and functions, like NSBeginAlertSheet, that
>
give the user an option to pass a temporary delegate and a callback
>
selector. How does Cocoa implement this?
Bill: First of all, I don't know how Cocoa implements anything. I'm going to describe how I implemented something, which might not be the thing you're trying to do, so if not, sorry about that.
I have a class A. I want an instance of any other class, let's call it B, to be able to ask an instance of class A to put up a sheet in a window specified by B and call back into B with the result when it's all over. In short, A should behave much the way NSBeginAlertSheet behaves; it acts as the supplier of the sheet and calls back when the sheet ends.
To do this, I naturally have the relevant method in A include parameters for an instance (in this case it will be B's self) and a method there, for A to call at the end of the process. I call these the "delegate" and the "selector", though of course this is not true delegation. So the call from B to A is structured a lot like how NSApp beginSheet is structured.
Now, as you know, sheets are handled in a three-part process. The relevant method in A, let's call it A-1, is just going to put up the sheet (with NSApp beginSheet) and then return. Another method in A, let's say A-2, is going to handle any button presses, and yet a third method in A, call it A-3, is going to be called when the sheet ends. The question is, how to get the selector info from method A-1 to method A-3, so that A-3 can call back to B.
The answer turns out to be the contextInfo. By packaging up the relevant info into a dictionary in A-1 and handing this (retained!) as the contextinfo to NSApp beginSheet, I am then able to unpackage it in A-3. Then A-3 can call the right method in B and communicate to it the result of the sheet (i.e. what the user did there). Thus, from B's point of view, it feels just like the two methods involved are the first and last stages of NSApp beginSheet (the call to beginSheet and the callback after the sheet ends). m.
matt neuburg, phd = email@hidden,
http://www.tidbits.com/matt
pantes anthropoi tou eidenai oregontai phusei
Subscribe to TidBITS! It's free and smart.
http://www.tidbits.com/
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.