正确使用 NSViewControllerrepresentedObject

发布于 2024-12-23 10:18:24 字数 329 浏览 6 评论 0原文

我有一个 NSViewController 管理一个 NSTableView 并注意到 NSViewController 有一个 representedObject 属性,但它不是一个IBOutlet 和我无法将 NSTableViewdataSource 绑定到界面生成器中 NSViewControllerrepresentedObject 属性。应该如何使用 returnedObject 属性?有没有正确使用的例子?

I have a NSViewController managing a NSTableView and noticed that NSViewController has a representedObject property, however it isn't an IBOutlet and I'm not able to bound the dataSource of NSTableView to the representedObject property of NSViewController in interface builder. How is representedObject property suppose to be used? Are there any examples of proper usage?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

神经大条 2024-12-30 10:18:24

representedObject 属性应设置为位于 nib 外部的对象,例如文档、另一个模型控制器或模型对象。 nib 中的事物应该从 VC 或 VC 的 representedObject 获取数据。

The representedObject property should be set to an object that lives outside of the nib, such as the document, another model-controller, or a model object. Things in the nib should get the data from the VC or the VC's representedObject.

不打扰别人 2024-12-30 10:18:24

我知道这是一个老话题,但我想我应该添加它,因为我对representedObject做了相当多的研究。希望这有帮助!

representedObject 是对视图应表示的某个 AnyObject (NSObject) 的引用。

它不是对象的副本,而是对其的引用(在 Swift 和 Objective-C 中)

理想情况下,如果相关视图是“联系人应用程序”的页面。此页面代表一个联系人,那么representedObject应该由实例化它的对象设置为fooContactfooContact 是对相关联系人实例的引用。

它不必由实例化类设置,但我个人发现它是一种更清晰的处理方法。

我通常避免尝试覆盖所表示对象的默认 getter/setter 并通过类中的另一个 var 引用它,即

weak var document: Document{
    if let docRef = self.representedObject as Document {
        return docRef
    }
    return nil
}

维护弱引用将避免引用循环。

I know this is an old topic, but I thought I'd add to it as I did quite a bit of research into representedObject. Hope this helps!

representedObject is a reference to some AnyObject (NSObject) that the view should represent.

It's NOT a copy of the object, but rather a reference to it (both in Swift and Objective-C)

Ideally speaking, if the view in question is a page out a "contacts app". This page represents a contact then the representedObject should be set to fooContact by the object that instantiated it. fooContact being a reference to an instance of the contact in question.

It doesn't have to be set by the instantiating class, but personally I find it a cleaner approach to things.

I generally avoid trying to override the default getters/setters of the representedbject and reference it by another var in the class i.e.

weak var document: Document{
    if let docRef = self.representedObject as Document {
        return docRef
    }
    return nil
}

maintaining the weak reference will avoid reference cycles.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文