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?
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
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With