Carelinkz
Carelinkz

Reputation: 936

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?

Upvotes: 3

Views: 3084

Answers (3)

Colas
Colas

Reputation: 3573

What about [[NSDocumentController sharedDocumentController] currentDocument] ?

Be careful nevertheless. Please read NSDocumentController currentDocument returning nil

Upvotes: 1

Stream
Stream

Reputation: 9493

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;

Upvotes: 6

Carelinkz
Carelinkz

Reputation: 936

For any sub windows that are part of the document, it turns out that it's very easy to make a very simple subclass of NSViewController and store the required information in there. These view controllers are set up within the main Document implementation so it is easy to pass the address of the NSDocument object. Any actual subview can then be controlled by a view controller that is a subclass of this "managing controller".

This solution does not work for every object, but it does take the biggest hurdle and solves my problem...

Upvotes: 0

Related Questions