结合跨越多个视图控制器选择在一个单一窗口界面 [英] Binding selection across multiple view controllers in a single window interface

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

问题描述

我有环绕我的头围绕如何在两个视图控制器勾了几NSArrayControllers的问题。我要同步源列表表视图的选择在第二个细节视图控制器更新值。

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.

我使用的是可可开发中央构建核心数据教程的为出发点,但打破了体系结构,以便有一个NSWindowController包含两个NSViewControllers:一个是在左边的职位表,一个用于右侧的细节后

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.

该NSWindowController子类有绑定到邮政实体,并指向一个只读managedObjectContext访问一个NSArrayController [NSApp表示代表] managedObjectContext]

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]

我初始化,然后在 windowDidLoad 法两种视图控制器。

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 的一个NSArrayController绑定到邮政实体及其管理对象上下文绑定到父窗口控制器的管理对象上下文(全部通过界面生成器)

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 的一个NSObjectController绑定到窗口控制器的管理对象上下文,并通过 postsListsViewController.postsArrayController.selection <绑定到窗口控制器内容对象/ code>。这似乎是一个真正的黑客苏茨基

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.

怎样才能得到它,以便更改选择在 MDVCPostsListViewController 的表视图将更新 MDVCPostDetailViewController ?我觉得我很接近,但我不知道什么是丢失或什么是采取的最佳路线。我觉得 postsListsViewController.postsArrayController.selection 绑定是非常哈克。希望有一个更好的办法。

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.

我上传出现此为那些preFER看code,而不是仅仅看我的描述样本项目。你可以从我的网站在的http://www.secondgearsoftware.com/attachments/stackoverflow_objectcontroller.zip

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?

从项目看一眼,看来你刚才添加MDVCMainWindowController的实例,每个NIB。这将是从一个实际运行的主窗口单独的实例。

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.

您需要在您的源列表表selectionIndexes绑定到邮政阵列控制器。否则,选择不会在控制器级别是已知的。

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.

我建议了移动阵列控制器到主窗口控制器。在你windowDidLoad方法然后你可以将它传递到列表和详细信息视图两种。该名单将结合arrangedObjects和selectionIndexes,详细视图将绑定到selection.someKey。

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.

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

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆