Re: Beginner Question
Re: Beginner Question
On Sun, Oct 13, 2002 at 02:57:55PM -0400, Rick Frank wrote:
>
I have a document based application with a table view. I have a dataSource
>
with the table view. The datasource is my document class. The document class
>
has a member object which is an array of data. (Sounds pretty simple).
>
>
For example, a document might look like
>
>
@interface MyDocument : NSDocument
>
{
>
>
NSMutableArray* myData;
>
>
>
}
Bill Cheeseman's book, when it comes out next month, should have
examples of MVC separation in 'real applications'. Take a look at the
first few Vermont Recipes on stepwise.com for an earlier version of
his stuff, but it should give you an idea in any case.
A NSDocument subclass is probably better thought of as a "controller"
(I put that in quotes because I just finished ranting about it, but
let's just go with the accepted Cocoa MVC definitions :) than a model.
In very simple cases you may want to store your data directly as
instance variables of NSDocument, but more likely you'll have model
classes that do that.
>
The example code that I've seen with this kind of design have delegates for
>
the NSTableView. In these examples, the delegates somehow have access to the
>
data - that is, they seem to just have access somehow to "myData", which is
>
an outlet.
Most likely myData will not be an outlet. You only need to mark an
instance variable as an outlet if you want to connect it from
Interface Builder.
>
When I create delegates for the table view, I create them to have a "myData"
>
outlet, but It's nil.
'create delegates'? You seem to have the wrong idea about what a
delegate is. For a given class, a delegate implements one or more of
the methods for an object of that class. This saves you from
subclassing the class in most cases. Typically, you can implement
event handling in a controller object with access to the model, rather
than having to embed model awareness in the view.
>
How does one get the delegate to have access to "myData"?
If you're trying to respond to user interface actions, the most likely
candidate for a NSTableView delegate is your NSWindowController
subclass. Apple's documentation suggests that NSWindowController
subclasses are not always needed, but I haven't ever written a
substantive application where it wasn't necessary.
NSDocument has a method:
- (void)makeWindowControllers;
which you can use to create one or more window controllers,
and NSWindowController has a method:
- (void)setDocument:(NSDocument *)document;
which gets called by the above method.
>
I my previous life, I would have done something like this to get at a
>
document from a GUI object
With delegation, instead of retrieving information like an associated
document directly from the GUI object, the GUI object's delegate is
set to an object that is already aware of the associated document.
--
=Nicholas Riley <email@hidden> | <
http://www.uiuc.edu/ph/www/njriley>
Pablo Research Group, Department of Computer Science and
Medical Scholars Program, University of Illinois at Urbana-Champaign
_______________________________________________
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.