Re: Getting a reference to the data model from a delegate
Re: Getting a reference to the data model from a delegate
- Subject: Re: Getting a reference to the data model from a delegate
- From: James DiPalma <email@hidden>
- Date: Sun, 19 Jan 2003 14:26:13 -0800
Rob's problem is getting a reference to his data model from a nib
instantiated object (his table view's delegate).
From: Bill Cheeseman <email@hidden>
Your question is a little confusing since you don't say (in the
excerpt I
saw) whether the delegate is the NSWindowController subclass. But it
doesn't
really matter.
Rob did say that his delegate is a separate object and that his window
controller is acting as his table view's data source. His current code
uses this technique:
From: Rob In der Maur <email@hidden>
As my datasource methods are defined in my dedicated window
controller class, ..., I'm using:
DataModel *dataModel = [[[aTable dataSource] document]
dataModel];
to give me a reference back to the data model. Works fine for the
moment....
Rob wants to move his data source code out of his window controller
code (a great idea):
But in order to tidy things up, I'm considering moving my datasource
methods also in a seperate class. With that I will lose my
'reference point' to the window controller and hence my document and
data model.
This statement defines his problem more clearly: Rob's code needs a
reference to his window controller to get access to his data; if (data
source != window controller), he can't find his window controller (is
there any way to get from window to window controller without using
your document controller?).
I'm guessing that his delegate is in a nib file (separate from his
document's main nib file) and that his window controller is this nib's
file's owner. Given an object in a document's main nib can find its
document and that Rob's window controller has connections to his table
view:
my dedicated window controller class, which (obviously) has an
outlet to the respective table
To solve Rob's problem, I suggested he use his nib's file's owner
somehow (I suggest that this object could be a window controller, a
document, or any other object because connecting to your data model is
a general problem and not just a problem in Rob's specific case where
his window controller is probably file's owner):
If this [file's owner] object is your window controller or your
document, then your problem is pretty much solved. If it is not, look
at how it gets instantiated and if you can ask this object for a
reference to your document.
Bill generalizes how to get an instance variable set up (which I'm
guessing is relevant here if a nib's file's owner is not a window
controller nor a document -- I'll have to go look at Vermont Recipes to
see how Bill solves this problem).
To generalize what Jim said, it is common in any object-oriented
programming
language to pass a reference to the creating class into the created
class as
a parameter in the instantiation or initialization routine.
An alternative is to create your MYClass instance first, then simply
tell it
to set the instance variable, like so: [myInstance setMyIVar:self];
It is also common for objects to get their object references
initialized using InterfaceBuilder's ability to connect outlets.
Rob already has code in his delegate that goes out and fetches his data
model (see above), this code could be in his delegate's awakeFromNib
method, or could just be in any of his delegate methods that need
access to data. Other ways to initialize instance variables for
nib-instantiated objects include a file's owner's awakeFromNib, a
window controller's windowDidLoad, or a document's
windowControllerDidLoadNib:.
I tend to have nib instantiated objects (like Rob's delegate and future
datasource) pull any references they need. I just feel more comfortable
not requiring a file's owner to be responsible for knowing what its nib
file's top-level objects need to get their work done (but I do give a
file's owner responsibility to provide a back reference to objects
outside its nib file).
-jim
_______________________________________________
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.