我有一个带有两个 NIB 的核心数据应用程序,一个带有一对 NSTableView 的主窗口和一个用于添加内容的表单。该表单有一个 NSTextView 和一个 NSTextField。我在核心数据中有两个实体,并使用两个 NSArrayControllers 管理内容。使用一个 NSArrayController,我的 AppDelegate 中的以下代码可以很好地添加内容:
id o = [bookController newObject];
[o setValue:@"Drafts" forKey:@"bookName"];
[o setValue:0 forKey:@"sortOrder"];
[bookController addObject:o];
但是,我的 AppController 类中的这段代码总是返回 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];
似乎 chapterArrayController 没有连接到核心数据中的 Chapter 实体,但 IB 中的绑定是正确的。我认为这与多个笔尖有关,但我在这里有点不知所措。
任何指向正确方向的指针都值得赞赏。
谢谢。
更新 2:我创建了一个名为 JBAddChapter 的类,如下所示:
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
我在主窗口的 IB 中创建了两个按钮,并将一个连接到上面的 testArrayControllers IBAction。我连接到 AppController 和这个 IBAction 的另一个按钮:
- (IBAction)testArrayControllers:(id)sender
{
[JBAddChapter getChapterData:nil withChapterText:nil];
}
如果我standAloneTestArrayControllers
从 JBAddChapter 的 IBAction 调用,一切正常。如果我使用 JBAddChapter 中的工厂方法从 AppController 调用相同的方法,则数组控制器为零。
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
那么,为什么数组控制器会从一种方法返回 nil 而不是另一种呢?据我所知,IB 中的所有绑定都是正确的。