I have a core data application with two NIBs, the main window with a pair of NSTableViews and a form for adding content. The form has an NSTextView and an NSTextField. I have two entities in core data, and manage the content with two NSArrayControllers. With one NSArrayController, the following code in my AppDelegate works fine for adding content:
id o = [bookController newObject];
[o setValue:@"Drafts" forKey:@"bookName"];
[o setValue:0 forKey:@"sortOrder"];
[bookController addObject:o];
But, this code in my AppController class always returns null:
NSObject *o = [chapterArrayController newObject];
[o setValue:contentOfchapter forKey:@"chapterText"];
[o setValue:chapterTitleString forKey:@"chapterTitle"];
[o setValue:@"Drafts" forKey:@"bookChapter"];
NSLog(@"Where is the object?: %@", o);
[chapterArrayController addObject:o];
It seems like the chapterArrayController is not connected to the Chapter entity in core data, but the bindings in IB are correct. I'm thinking that this has something to do with the multiple nibs, but I'm kind of at a loss here.
Any pointers in the right direction are appreciated.
Thanks.
Update 2: I created a class named JBAddChapter, which looks like this:
JBAddChapter.h
#import <Cocoa/Cocoa.h>
@interface JBAddChapter : NSObject {
IBOutlet NSArrayController *bookController;
IBOutlet NSArrayController *chapterArrayController;
}
- (IBAction)testArrayControllers:(id)sender;
+ (void)getChapterData:(NSString *)passedChapterTitle withChapterText:(NSString *)passedChapterText;
- (void)standAloneTestArrayControllers;
@end
JBAddChapter.m
#import "JBAddChapter.h"
@implementation JBAddChapter
+ (void)getChapterData:(NSString *)passedChapterTitle withChapterText:(NSString *)passedChapterText;
{
[[self alloc] standAloneTestArrayControllers];
}
- (IBAction)testArrayControllers:(id)sender
{
[self standAloneTestArrayControllers];
}
- (void)standAloneTestArrayControllers
{
[chapterArrayController fetchWithRequest:nil merge:NO error:nil];
NSLog(@"1. chapterArrayController %@", chapterArrayController);
NSArray *a = [chapterArrayController arrangedObjects];
NSLog(@"2. NSArray a = %@", a);
NSUInteger numberOfMenuItems = [a count];
NSLog(@"3. Count of items in array: %d", numberOfMenuItems);
[bookController fetchWithRequest:nil merge:NO error:nil];
NSLog(@"1. bookController %@", chapterArrayController);
NSArray *b = [chapterArrayController arrangedObjects];
NSLog(@"2. NSArray b = %@", b);
NSUInteger newNumberOfMenuItems = [a count];
NSLog(@"3. Count of items in array: %d", newNumberOfMenuItems);
}
@end
I've created two buttons in IB in my main window, and hooked one up to the testArrayControllers IBAction above. The other button I hook up to AppController and this IBAction:
- (IBAction)testArrayControllers:(id)sender
{
[JBAddChapter getChapterData:nil withChapterText:nil];
}
If I call the standAloneTestArrayControllers
from JBAddChapter's IBAction, everything works fine. If I call that same method from AppController using the a factory method in JBAddChapter, I the array controllers are nil.
2010-01-07 06:15:36.971 Scout[3881:a0f] 1. chapterArrayController <NSArrayController: 0x200060b40>[entity: Chapter, number of selected objects: 1]
2010-01-07 06:15:36.972 Scout[3881:a0f] 2. NSArray a = (
loads of stuff
)
2010-01-07 06:15:36.973 Scout[3881:a0f] 3. Count of items in array: 9
2010-01-07 06:15:36.974 Scout[3881:a0f] 1. bookController <NSArrayController: 0x200060b40>[entity: Chapter, number of selected objects: 1]
2010-01-07 06:15:36.978 Scout[3881:a0f] 2. NSArray b = (
loads of stuff
)
2010-01-07 06:15:36.979 Scout[3881:a0f] 3. Count of items in array: 8
2010-01-07 06:15:38.402 Scout[3881:a0f] 1. chapterArrayController (null)
2010-01-07 06:15:38.402 Scout[3881:a0f] 2. NSArray a = (null)
2010-01-07 06:15:38.402 Scout[3881:a0f] 3. Count of items in array: 0
2010-01-07 06:15:38.403 Scout[3881:a0f] 1. bookController (null)
2010-01-07 06:15:38.403 Scout[3881:a0f] 2. NSArray b = (null)
2010-01-07 06:15:38.403 Scout[3881:a0f] 3. Count of items in array: 0
So, why would the array controllers be returning nil from one method, but not the other? All bindings in IB are correct, as far as I can tell.