NSView bindings coredata

Discussion in 'Mac Programming' started by MrFusion, Aug 8, 2006.

  1. MrFusion macrumors 6502a

    Jun 8, 2005
    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:mad:"myView"
    managedObjectContext = [theMOC retain];
    NSManagedObject *mo= [NSEntityDescription insertNewObjectForEntityForName:mad:"myEntity"
    [mo setValue:mad:"test 1"
    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?
  2. MrFusion thread starter macrumors 6502a

    Jun 8, 2005
    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:eek:wner: in your init method.
    //init your nsview
    myview = [[viewController alloc] init];
    //set the moc
    [myview setvalue:moc forkey:mad:"moc"];
    add the nsview to your superview

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

Share This Page