NSView bindings coredata

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

  1. macrumors 6502a

    Joined:
    Jun 8, 2005
    Location:
    West-Europe
    #1
    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"
    owner:self];
    //model
    managedObjectContext = [theMOC retain];
    NSManagedObject *mo= [NSEntityDescription insertNewObjectForEntityForName:mad:"myEntity"
    inManagedObjectContext:managedObjectContext];
    [mo setValue:mad:"test 1"
    forKey:mad:"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
     
  2. thread starter macrumors 6502a

    Joined:
    Jun 8, 2005
    Location:
    West-Europe
    #2
    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