PDA

View Full Version : NSView bindings coredata




MrFusion
Aug 8, 2006, 09:52 AM
I am trying the following:
I have a NSView (in its own nib file), that use the NSManagedObjectContext (moc) from the main program. I want to reuse this view in multiple windows.

-(id) initWithContext:(NSManagedObjectContext *) theMOC
{
self = [super init];
if (self){
[NSBundle loadNibNamed:@"myView"
owner:self];
//model
managedObjectContext = [theMOC retain];
NSManagedObject *mo= [NSEntityDescription insertNewObjectForEntityForName:@"myEntity"
inManagedObjectContext:managedObjectContext];
[mo setValue:@"test 1"
forKey:@"title"];
return self;
}
return nil;
}

I can load this NSView and display it in a window and add entries to the moc (in code). But the interface elements (such as a table) complain that there is no moc:
'Cannot perform operation without a managed object context'

I have checked the bindings. The arraycontroller is bound to 'theMOC'.

Any ideas?
Thanks



MrFusion
Aug 8, 2006, 10:12 AM
I am trying the following:
I have a NSView (in its own nib file), that use the NSManagedObjectContext (moc) from the main program. I want to reuse this view in multiple windows.

-(id) initWithContext:(NSManagedObjectContext *) theMOC
{
self = [super init];
if (self){
[NSBundle loadNibNamed:@"myView"
owner:self];
//model
managedObjectContext = [theMOC retain];
NSManagedObject *mo= [NSEntityDescription insertNewObjectForEntityForName:@"myEntity"
inManagedObjectContext:managedObjectContext];
[mo setValue:@"test 1"
forKey:@"title"];
return self;
}
return nil;
}

I can load this NSView and display it in a window and add entries to the moc (in code). But the interface elements (such as a table) complain that there is no moc:
'Cannot perform operation without a managed object context'

I have checked the bindings. The arraycontroller is bound to 'theMOC'.

Any ideas?
Thanks

Never mind.
It was once again a question of when to set a value: in the init or after the outlets have been set.
So: load the nsview (from the nib) with loadNibNamed:owner: in your init method.
//init your nsview
myview = [[viewController alloc] init];
//set the moc
[myview setvalue:moc forkey:@"moc"];
add the nsview to your superview

I am not sure its nice code or class design, but it works.