Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

MrFusion

macrumors 6502a
Original poster
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 said:
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😱wner: 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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.