I am having an issue wrapping my head around how to hook up a few NSArrayControllers across two view controllers. I want to sync the selection in the source list table view to update the values in the second detail view controller.
I'm using the Cocoa Dev Central Build A Core Data Tutorial as the starting point, but have broke down the architecture so that there is an NSWindowController that contains two NSViewControllers: one for the posts table on the left and one for the post details on the right.
The NSWindowController subclass has an NSArrayController that is bound to the Post entity and a read-only managedObjectContext accessor that points to [[NSApp delegate] managedObjectContext]
I then initializing the two view controllers in the windowDidLoad
method.
- (void)windowDidLoad
{
static NSInteger kSourceListViewIndex = 0;
static NSInteger kDetailViewIndex = 1;
self.postsListsViewController = [[MDVCPostsListViewController alloc] initWithWindowController:self];
NSView *sourceListSplitViewContentView = [[self.splitView subviews] objectAtIndex:kSourceListViewIndex];
NSView *sourceListView = [self.postsListsViewController view];
[sourceListView setFrame:[sourceListSplitViewContentView bounds]];
[sourceListView setAutoresizingMask:(NSViewWidthSizable | NSViewHeightSizable)];
[sourceListSplitViewContentView addSubview:sourceListView];
// And now let's load the detail view.
self.postDetailViewController = [[MDVCPostDetailViewController alloc] initWithWindowController:self];
NSView *detailSplitViewContentView = [[self.splitView subviews] objectAtIndex:kDetailViewIndex];
NSView *detailView = [self.postDetailViewController view];
[detailView setFrame:[detailSplitViewContentView bounds]];
[detailView setAutoresizingMask:(NSViewWidthSizable | NSViewHeightSizable)];
[detailSplitViewContentView addSubview:detailView];
}
MDVCPostsListViewController
has an NSArrayController bound to the Post entity and its managed object context bound to the parent window controller's managed object context (all through Interface Builder)
MDVCPostDetailViewController
has an NSObjectController bound to the window controller's managed object context and the content object bound to the window controller via postsListsViewController.postsArrayController.selection
. This seems like a really sucky hack.
How can I get it so that changing the selection in MDVCPostsListViewController
's table view will update the selected values in MDVCPostDetailViewController
? I feel like I'm close, but am not sure what's missing or what is the best route to take. I do feel that the postsListsViewController.postsArrayController.selection
binding is extremely hacky. Hopefully there's a better way.
I've uploaded my sample project that exhibits this for those that prefer to look at code rather than just read descriptions. You can grab it from my site at http://www.secondgearsoftware.com/attachments/stackoverflow_objectcontroller.zip