Core Data serperate Nibs

Discussion in 'Mac Programming' started by MacDonaldsd, Sep 7, 2007.

  1. MacDonaldsd macrumors 65816

    MacDonaldsd

    Joined:
    Sep 8, 2005
    Location:
    London , UK
    #1
    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 ?
     
  2. Krevnik macrumors 68030

    Krevnik

    Joined:
    Sep 8, 2003
    #2
    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?
     
  3. MacDonaldsd thread starter macrumors 65816

    MacDonaldsd

    Joined:
    Sep 8, 2005
    Location:
    London , UK
    #3
    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
     
  4. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #4
    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.
     
  5. MacDonaldsd thread starter macrumors 65816

    MacDonaldsd

    Joined:
    Sep 8, 2005
    Location:
    London , UK
    #5
    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
     
  6. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #6
    Are you actually calling your new method? Plus you need to set it up with an argument for it to make sense.
     
  7. Krevnik macrumors 68030

    Krevnik

    Joined:
    Sep 8, 2003
    #7
    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).
     
  8. MacDonaldsd thread starter macrumors 65816

    MacDonaldsd

    Joined:
    Sep 8, 2005
    Location:
    London , UK
    #8
    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.
     
  9. Krevnik macrumors 68030

    Krevnik

    Joined:
    Sep 8, 2003
    #9
    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.
     

Share This Page