# Objective C/Cocoa - document needs to get info from a data source - how?



## ksignorini (Dec 7, 2003)

Please help if you can.  I don't have much hair left.

I have a project with a document called iDMDocument, a window controller called iDMController and on my main document window I have a tableview that uses an NSObject called CombatTableDataSource as its data source.

The trouble is that in order to archive the document's data, I first need to sync up the data in the data source (a single array of other objects) with an identical array in my document object.  At that point I can use the archiving methods to save the data to a file.

The problem is that I have no idea how to get my iDMDocument to talk to the CombatTableDataSource object.  I simply need it to pass the array from the data source to the document.

Can anyone help?  I'm desperate.


(As a side note, it was no problem to get the window controller object to pass its data by using [[self windowControllers] makeObjectsPerformSelectorselector(prepareForSave)] to call a prepareForSave method in the window controller which then calls a method in [self document] to set the data in a variable in a document.)  -  this was courtesy of Building Cocoa Applications A Step-by-Step Guide by O'Reilly.


Thanks,
Kent!


----------



## ksignorini (Dec 7, 2003)

I got it!

From inside the window controller, this reference let me access the data source:

NSMutableArray *combatants = [[[self combatTable] dataSource] combatants];

Kent!


----------



## anarchie (Dec 7, 2003)

Instead of maintaining two separate arrays for each of your document and your data source, why not instead have your data source query the document for its array whenever it is needed?  You can make the connection between the two objects in Interface Builder.  If you've got it set up right, the iDMDocument should be your document nib's "File's Owner".


----------



## ksignorini (Dec 7, 2003)

anarchie said:
			
		

> Instead of maintaining two separate arrays for each of your document and your data source, why not instead have your data source query the document for its array whenever it is needed?  You can make the connection between the two objects in Interface Builder.  If you've got it set up right, the iDMDocument should be your document nib's "File's Owner".



I actually tried using the window controller as the data source at first (since I subclassed NSWindowController, my file's owner is the window controller) but I bunged it up and decided to follow some code examples that had a seperate data source (this is my first time programming in Cocoa/Objective C).

Seems to work, now, although it's probably not the best solution.  Next time, I'll try using the window controller as the data source again.

Thanks,
Kent!


----------



## ksignorini (Dec 7, 2003)

On second look, I think I will try to move all of this to the window controller object since it makes more sense to have the data there.  I mean, I already have to synchronize the data between the window controller (the active data) and the document (the data to be saved/loaded) since I have a seperate window controller.  Having a second control object (the data source for the table) just doesn't make much sense.  There will be even more data passing/synchronizing once I start using the table row to select data to go into other fields on the page.

Thanks for the help.  An outsider's perspective is always welcome to me.

Kent!


----------



## anarchie (Dec 7, 2003)

You shouldn't move any of that to your window controller object.  Its job should be to create and manage windows for the document, nothing more.  If it needs data that the document maintains, it should query the document for it via accessors whenever it is needed, rather than having to go about "synchronizing" everything...


----------



## ksignorini (Dec 7, 2003)

anarchie said:
			
		

> You shouldn't move any of that to your window controller object.  Its job should be to create and manage windows for the document, nothing more.  If it needs data that the document maintains, it should query the document for it via accessors whenever it is needed, rather than having to go about "synchronizing" everything...



OK, well, I can move the datasource data and all the datasource methods to the document, but how can I get the tableView to use the document as the datasource?  As per the earlier discussion, since the windowcontroller object is the File Owner, how can I "draw" the connection from the tableview to the document acting as the datasource?

Or should the tableView methods reside in the windowcontroller but access the data array that exists in the document?  Maybe that's the way to go?   Is this what you're refering to?

Thanks,
Kent!


----------



## ksignorini (Dec 7, 2003)

YES!

I DID IT!

I moved everything to the document itself, and boy-oh-boy, is that better.

Of course, I had some trouble at first since the methods a document runs on load are different than those a window controller or a data source runs.  I had to figure out where to get the document to restore its window's text fields, etc. after it loads saved data (- (void)windowControllerDidLoadNibNSWindowController *) aController).

Anyway, thanks for all your help.  This is a better place for everything.  I feel like an a%% for doing it the other way in the first place, but the book I was reading suggests that (paraphrasing here) 'since you're going to want to subclass NSWindowController at some point, you might as well do it now and learn how to do it....'  

Yeah, right.

Next, I gotta figure out how to get the tabing order correct for my on-screen window text fields.  Hitting TAB just moves me from field to field, going across the screen.  I want to change that.  What to do, oh what to do...

Thanks,
Kent!


----------



## anarchie (Dec 8, 2003)

Setting up the tabbing order is fairly easy.  Connect the 'nextKeyView' outlet of each field to the field that should follow it in the order.  It's also advisable to link in buttons and other interface elements, so that Full Keyboard Access users can access them.  They will be skipped if FKA is disabled.


----------



## ksignorini (Dec 8, 2003)

What's funny is that I did connect the nextKeyView's and the order didn't change.  I thought maybe I didn't understand how it works.

I'll have to try again.

Thanks,
Kent!


----------

