在多个故事板场景中使用NSArrayController [英] Using an NSArrayController in Multiple Storyboard Scenes

查看:176
本文介绍了在多个故事板场景中使用NSArrayController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于Mac文档的Core Data应用程序,它使用故事板。故事板具有以下布局:

I have a Mac document-based Core Data application that uses storyboards. The storyboard has the following layout:

Window Controller
    Split View Controller
        Table View Controller
        Text View Controller

我的核心数据模型包含一个Chapter实体,包含两个属性:title和contents。我想让表视图显示每个章节的标题。文本视图显示所选章节的内容。

My Core Data model contains a Chapter entity that contains two attributes: title and contents. I want the table view to show each chapter title. The text view shows the contents of the selected chapter.

如果我使用xib文件,我将向xib文件添加一个数组控制器。我将绑定数组控制器到文件的所有者访问我的NSPersistentDocument子类。我会将表视图绑定到数组控制器的arrangedObjects属性,并将文本视图绑定到数组控制器的选择。

If I was using a xib file, I would add an array controller to the xib file. I would bind the array controller to File's Owner to access my NSPersistentDocument subclass. I would bind the table view to the array controller's arrangedObjects property and bind the text view to the array controller's selection.

但是对于storyboards,事情变得更复杂。我可以添加一个数组控制器到表视图控制器,绑定表视图到数组控制器,并使章节标题显示在表视图中。但是文本视图控制器不能绑定到该数组控制器,因为数组控制器在另一个场景中。

But with storyboards things get more complicated. I can add an array controller to the table view controller, bind the table view to the array controller, and have the chapter titles show up in the table view. But the text view controller can't bind to that array controller because the array controller is in another scene.

如何在Interface Builder中添加数组控制器,表视图控制器和文本视图控制器可以访问它并绑定到它?

How do I add an array controller in Interface Builder so that both the table view controller and text view controller can access it and bind to it?

推荐答案

NSViewController 子类中的 NSArrayController 实例,并通过中央数据源将它们绑定在一起(很可能是您的NSDocument子类)。然后,您可以将此数据源设置为您的 NSViewController 子类resentObject ,将其传递到您的降序
控制器。这是一个使用 NSWindowController 的故事板应用程序的示例,它有一个内容视图控制器,它是一个 NSSplitViewController 视图控制器(A Master / Detail设置):

The key to making this work is to have a NSArrayController instance in each of your descending NSViewController subclasses and binding them together through a central data source (most likely your NSDocument subclass). You can then set this data source as your NSViewController subclasses representedObject by passing it down through your descending controllers. Here is an example of a storyboard application with an NSWindowController which has a content view controller that is a NSSplitViewController with two child view controllers (A Master / Detail setup):

class Document: NSDocument {

    var dataSource: DataSource? = DataSource()

    ...
}

class DataSource: NSObject, NSCoding {

    var items: [Item] = []
    var selectionIndexes: NSIndexSet = NSIndexSet()

    ...
}

class WindowController: NSWindowController {

    override var document: AnyObject? {
        didSet {
            if let document = self.document as? Document {
                self.contentViewController?.representedObject = document
            }
        }
    }

}

class SplitViewController: NSSplitViewController {

    override var representedObject: AnyObject? {
        didSet {
            for viewController in self.childViewControllers as! [NSViewController] {
                viewController.representedObject = representedObject
            }
        }
    }
}

诀窍是将resentObject 绑定到每个降序视图控制器的 NSArrayController 在故事板。您需要绑定不仅仅是 contentArray ,还有 selectionIndexes

The trick is to bind the representedObject to each of your descending view controller's NSArrayController in the storyboard. You need to bind NOT ONLY the contentArray BUT ALSO the selectionIndexes.

结果是 selectionIndexes 在下降的 NSArrayController 中保持同步,因为它们是绑定的中心数据源(上面的例子中 DataSource 子类)。

The result is that the selectionIndexes on both descending NSArrayControllers are kept in sync because they are bound through the central data source (DataSource subclass in above example).

为了使这一切更清楚,我创建了一个例子项目,在此演示: https://github.com/acwright/StoryboardBindingsExample

To make this all clearer I have created an example project that demonstrates this here: https://github.com/acwright/StoryboardBindingsExample

这篇关于在多个故事板场景中使用NSArrayController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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