Search code examples
cocoansdocument

How to access an object's NSDocument?


I can access an app-wide delegate instance using [NSApp delegate] after adding an NSObject to the mainmenu.xib, setting the name of the object to the name of my appDelegate and setting the mainmenu.xib delegate to this object.

Now, what I would like to do, is to access to an object's Document, i.e. the active NSDocument that the object "belongs" to. It would be a doc-wide delegate instance I guess. Sometimes [self document] works, but not always. Is there a generic way?


Solution

  • There is no need to pass a reference explicitly. You may access the document from NSViewController in the following way:

    id document = self.view.window.windowController.document;