I'm working on my first Mac document-based application.
I have subclassed NSDocument
, reimplementing methods such as
- (BOOL)readFromURL:(NSURL *)absoluteURL ofType:(NSString *)typeName error:(NSError **)outError;
- (BOOL)writeToURL:(NSURL *)absoluteURL ofType:(NSString *)typeName error:(NSError **)outError;
- (void)makeWindowControllers;
The main window controller is a subclass of NSWindowsController
, that contains two NSViewController subclasses.
The problem I'm facing is that I need to have access to the current document from these view controllers. What I do is calling
MyDocument *myDocument = [[NSDocumentController sharedController] currentDocument];
At first, right after starting the application, a new document is created. Then, the main window and its view controllers are created, but the method above returns nil. Here's the log (using NSLog) I get:
Just created this new document: <MyDocument: 0x10040ff10>
I'm in a view controller and current document is (null)
After that, creating a new document and calling this method results in a non-nil pointer, but it doesn't point at the right document, but to the first one:
Just created this new document: <MyDocument: 0x100437e10>
I'm in a view controller and current document is <MyDocument: 0x10040ff10>
Notice that after the second document creation, currentDocument
points to the first document and not to the second one.
Any idea of what I'm missing or doing wrong here? When is currentDocument
set for NSDocumentController
?