Mac Core Data serperate Nibs

MacDonaldsd

macrumors 65816
Original poster
Sep 8, 2005
1,009
0
London , UK
The problem is I am unable to use an Entity form my data model in a separate nib file (in the same project) but it works fine in mainmenu.nib.

If I option drag the Entity from the data model on to my separate nib file it compiles and everything looks fine, but when you click on the add button nothing happens.

How would I solve this ?
 

Krevnik

macrumors 68040
Sep 8, 2003
3,237
645
The problem is I am unable to use an Entity form my data model in a separate nib file (in the same project) but it works fine in mainmenu.nib.

If I option drag the Entity from the data model on to my separate nib file it compiles and everything looks fine, but when you click on the add button nothing happens.

How would I solve this ?
You need to find a way to link the separate NIB to your managed object context. How you split the files will determine how you need to do this, but what I do is that the second nib's file owner in my project is an NSObjectController (the second NIB provides a set of views used to interact with a single managed object), and bind any controller's Managed Object Context to the managedObjectContext key path of the file owner.

I could probably be a bit more specific if I knew a bit more about the project:

- Is this an NSDocument-based app?
- What is the file owner for this second NIB?
 

MacDonaldsd

macrumors 65816
Original poster
Sep 8, 2005
1,009
0
London , UK
Its not a Doument based app and the file owner of the Second nib is the following class

@interface ModuleEditor : NSWindowController {
}

@end

#import "ModuleEditor.h"


@implementation ModuleEditor

-(id)init
{
self =[super initWithWindowNibName:mad:"ModuleEditor"];
return self;
}

-(void)windowDidLoad
{
NSLog(@"ModualEditor Nib file is loaded");
}

@end
 

kainjow

Moderator emeritus
Jun 15, 2000
7,745
3
CoreData objects aren't anything special. Just create accessors in ModuleEditor for accessing the NSManagedObjectContext object and then you can bind to it in the nib.

I'd suggest creating an initWithManagedObjectContext: method in ModuleEditor, which then loads the nib.
 

MacDonaldsd

macrumors 65816
Original poster
Sep 8, 2005
1,009
0
London , UK
if I change Module editor to

@implementation ModuleEditor

-(id)initWithManagedObjectContext
{
self =[super initWithWindowNibName:mad:"ModuleEditor"];
return self;
}

-(void)windowDidLoad
{
NSLog(@"ModualEditor Nib file is loaded");
}

@end

It doesn't load the nib at all
 

kainjow

Moderator emeritus
Jun 15, 2000
7,745
3
Are you actually calling your new method? Plus you need to set it up with an argument for it to make sense.
 

Krevnik

macrumors 68040
Sep 8, 2003
3,237
645
if I change Module editor to

@implementation ModuleEditor

-(id)initWithManagedObjectContext
{
self =[super initWithWindowNibName:mad:"ModuleEditor"];
return self;
}

-(void)windowDidLoad
{
NSLog(@"ModualEditor Nib file is loaded");
}

@end

It doesn't load the nib at all
To add to what kainjow has said, you also need to make it accessible:

Code:
-(id)initWithManagedObjectContext:(NSManagedObjectContext *)context
{
    self =[super initWithWindowNibName:@"ModuleEditor"];
    if( self != nil )
    {
        managedObjectContext = [context retain];
    }
    return self;
}

- (void)dealloc
{
    [managedObjectContext release];
    [super dealloc];
}

- (NSManagedObjectContext)managedObjectContext
{
    return managedObjectContext:
}
And of course, managedObjectContext is an instance variable in your NSWindowController subclass.

The odds are that if it isn't loading the window, then there are errors being spewed to the run log. So check that for specifics, but these are the errors we see right off the bat. Without the ability to set/retrieve the managed object context, your ModuleEditor NIB won't be able to actually bind to your managed object context to make changes to the document. So things like array controllers should have their Managed Object Context bound to the file owner, and have 'managedObjectContext' as the key path (which will return the instance variable in your subclass).
 

MacDonaldsd

macrumors 65816
Original poster
Sep 8, 2005
1,009
0
London , UK
thanks Krevnik and kainjow your help is very much appreciated.

after Kainjows advice I managed to find some information about initWithManagedObjectContext but the problem is that I was not retaining it.

Now it all works fine :D

Can i ask why you have to retain it ?

managedObjectContext = [context retain];

My main point of reference is Hillegass book which doesn't have any core data.
 

Krevnik

macrumors 68040
Sep 8, 2003
3,237
645
thanks Krevnik and kainjow your help is very much appreciated.

after Kainjows advice I managed to find some information about initWithManagedObjectContext but the problem is that I was not retaining it.

Now it all works fine :D

Can i ask why you have to retain it ?

managedObjectContext = [context retain];

My main point of reference is Hillegass book which doesn't have any core data.
It is the way memory management in Obj-C works. When you take in an object you want to keep around in your object, you retain it. When your object is deallocated or you are done with the object you release it. This ensures the object stays around when you need it.

Don't assume any object returned from an accessor is retained, assume it has been autoreleased.