正确使用 NSViewControllerrepresentedObject
我有一个 NSViewController
管理一个 NSTableView
并注意到 NSViewController
有一个 representedObject
属性,但它不是一个IBOutlet 和我无法将 NSTableView
的 dataSource
绑定到界面生成器中 NSViewController
的 representedObject
属性。应该如何使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
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'srepresentedObject
.我知道这是一个老话题,但我想我应该添加它,因为我对representedObject做了相当多的研究。希望这有帮助!
representedObject
是对视图应表示的某个AnyObject
(NSObject
) 的引用。它不是对象的副本,而是对其的引用(在 Swift 和 Objective-C 中)
理想情况下,如果相关视图是“联系人应用程序”的页面。此页面
代表一个联系人
,那么representedObject应该由实例化它的对象设置为fooContact
。fooContact
是对相关联系人实例的引用。它不必由实例化类设置,但我个人发现它是一种更清晰的处理方法。
我通常避免尝试覆盖所表示对象的默认 getter/setter 并通过类中的另一个 var 引用它,即
维护弱引用将避免引用循环。
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 someAnyObject
(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 tofooContact
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.
maintaining the weak reference will avoid reference cycles.