Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper usage of NSViewController representedObject

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?

like image 879
Tony Avatar asked Dec 26 '11 21:12

Tony


2 Answers

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.

like image 74
Peter Hosey Avatar answered Oct 31 '22 03:10

Peter Hosey


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.

like image 24
Adrian Sluyters Avatar answered Oct 31 '22 03:10

Adrian Sluyters