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

查看:34
本文介绍了在多个故事板场景中使用 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

我的核心数据模型包含一个章节实体,该实体包含两个属性:标题和内容.我希望表格视图显示每个章节的标题.文本视图显示所选章节的内容.

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 文件中添加一个数组控制器.我会将数组控制器绑定到 File's Owner 以访问我的 NSPersistentDocument 子类.我会将表格视图绑定到数组控制器的排列对象属性,并将文本视图绑定到数组控制器的选择.

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.

但是有了故事板,事情就会变得更加复杂.我可以向表视图控制器添加一个数组控制器,将表视图绑定到数组控制器,并在表视图中显示章节标题.但是文本视图控制器无法绑定到该数组控制器,因为该数组控制器在另一个场景中.

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 子类 representedObject控制器.这是一个带有 NSWindowController 的故事板应用程序示例,它有一个内容视图控制器,它是一个 NSSplitViewController 和两个子视图控制器(主/细节设置):>

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
            }
        }
    }
}

诀窍是将 representedObject 绑定到故事板中每个降序视图控制器的 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.

结果是两个降序 NSArrayController 上的 selectionIndexes 保持同步,因为它们通过中央数据源(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天全站免登陆