RE: Saving NSObjectController info
RE: Saving NSObjectController info
- Subject: RE: Saving NSObjectController info
- From: <email@hidden>
- Date: Wed, 16 Feb 2005 6:43:47 +0000
mmalc wrote:
I'd expect your MyDocument class to have an instance variable for the
object that the object controller is managing? Assuming this is the
case, the fact that the object is not referenced elsewhere in your code
is irrelevant. The object controller"simply" serves as a mediator
between the model object it "represents" and the view.
If this is not the case (that is, you don't have an instance variable
for the object), I would probably suggest that you add one so that it
works as described above. In general it's probably better not to have
"free-floating" objects in your nib.
Okay, I apologize, but I might need someone to help me along here. I know bindings and such have been around for a while, but I've been away from Cocoa for a while now, and I'm trying to get back into it.
I've added an instance variable of my model object in MyDocument. Now, I obviously don't want MyDocument to be creating new model objects in awakeFromNib: or init: as the NSObjectController takes care of instantiating objects as it needs them, correct?
So I have the variable declared. In IB, I've added an outlet to File's Owner (MyDocument) of my model type, and made a connection between the two. So now MyDocument has a connection to my model object, and the NSObjectController's content outlet is set to my model object (in IB). So all should work, right?
Wrong. I ran my app (it built with no errors), entered a bunch of stuff in my fields, saved the file to disk, opened it back up and the fields were empty. Here is some code that I have. The model object is called Recipe (this is a recipe management app for my wife, BTW):
First off, the Recipe object is declared in MyDocument.h (Recipe* recipe;), but I don't want to instantiate it in MyDocument.m, that's the ObjectController's job, right? So here is the method to save the object to disk:
- (void)dataRepresentationOfType:(NSString*)aType
{
return [NSArchiver archivedDataWithRootObject:recipe];
}
Here is the load data method:
- (void)windowControllerDidLoadNib:(NSWindowController*)aController
{
Recipe* newRecipe = [[NSUnarchiver unarchiveObjectWithData:fileData] retain]; // fileData is my NSData* object
recipe = newRecipe;
[newRecipe release];
}
Now, before, instead of "recipe = newRecipe", I had my object controller use setContent:newRecipe, and it worked fine.
I'm obviously missing something here. I'm just trying to understand this, so any help would be appreciated. Thanks.
James
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden