本文介绍了在单个窗口界面中跨多个视图控制器绑定选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题,围绕如何在两个视图控制器上连接几个NSArrayController。我想同步源列表表视图中的选择以更新第二个详细视图控制器中的值。



我使用的是作为起点,但已经打破了架构,所以有一个NSWindowController包含两个NSViewController:一个用于左侧的帖子表,一个用于右侧的帖子详细信息。 p>

NSWindowController子类有一个绑定到Post实体的NSArrayController和一个只读的managedObjectContext访问器,它指向 [[NSApp delegate] managedObjectContext] / code>



然后在 windowDidLoad 方法中初始化两个视图控制器。

   - (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];

//现在让我们加载详细视图。
self.postDetailViewController = [[MDVCPostDetailViewController alloc] initWithWindowController:self];
NSView * detailSplitViewContentView = [[self.splitView subviews] objectAtIndex:kDetailViewIndex];
NSView * detailView = [self.postDetailViewController view];
[detailView setFrame:[detailSplitViewContentView bounds]];
[detailViewAutoresizingMask:(NSViewWidthSizable | NSViewHeightSizable)];
[detailSplitViewContentView addSubview:detailView];
}

MDVCPostsListViewController NSArrayController绑定到Post实体及其受管对象上下文绑定到父窗口控制器的管理对象上下文(全部通过Interface Builder)



MDVCPostDetailViewController 有一个绑定到窗口控制器的管理对象上下文的NSObjectController和通过 postsListsViewController.postsArrayController.selection 绑定到窗口控制器的内容对象。



我如何得到它,改变在 MDVCPostsListViewController 的选择表视图将更新 MDVCPostDetailViewController 中的选定值?我觉得我很近,但不知道缺少什么或什么是最好的路线。我觉得 postsListsViewController.postsArrayController.selection 绑定是非常hacky。希望有一个更好的方法。



我已经上传我的示例项目,为那些喜欢看代码,而不是只读说明。您可以从我的网站

解决方案

您希望每个窗口控制器发现主窗口控制器?



从项目快速浏览,似乎你刚刚添加一个MDVCMainWindowController的实例到每个NIB。这些将是实际运行主窗口的实例。



您需要将源列表表selectionIndexes绑定到Post数组控制器。否则,在控制器级别将不会知道选择。



我建议将数组控制器移动到主窗口控制器。在您的windowDidLoad方法中,您可以将其传递到列表和详细视图。该列表将绑定到arrangeObjects和selectionIndexes,详细信息视图将绑定到selection.someKey。


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

解决方案

How do you expect each window controller to discover the main window controller?

From a quick glance at the project, it seems you just added an instance of MDVCMainWindowController to each NIB. These will be separate instances from the one actually running the main window.

You need to bind your source list table selectionIndexes to the the Post array controller. Otherwise the selection will not be known at the controller level.

I would suggest moving the array controller up into the main window controller. In your windowDidLoad method you could then pass it down to both the list and detail view. The list would bind to arrangedObjects and selectionIndexes, the detail view would bind to selection.someKey.

这篇关于在单个窗口界面中跨多个视图控制器绑定选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 06:49