0

I'm writing a Cocoa application that loads data from a file. This involves overriding readFromData:ofType:error:. I'm curious about how people deal with outlets not yet being connected when readFromData:ofType:error: is called. The documentation from Apple states this:

... you must remember that the NSDocument data-reading methods, such as readFromData:ofType:error:, are called before the document’s user interface objects contained in its nib file are loaded. Of course, you cannot send messages to user interface objects until after the nib file loads.

What's the benefit of loading the UI objects after readFromData:ofType:error: is called? I assume this loading order is the reason my outlets are nil. Since the outlets are not yet loaded, I'm finding that I need create extra copies of variables to store data that would then get stored in those UI elements that are created after this method ends.

JaredH
  • 2,338
  • 1
  • 30
  • 40
Elector Niklas
  • 151
  • 1
  • 9

1 Answers1

0

Separation of model and view code. readFromData:ofType:error: is strictly for trying to read a document's data and being able to signal if the operation succeeded or failed.

Furthermore, in my opinion, NSDocument subclasses are not actually suppose to have outlets or UI code. One can think of NSDocument as a controller for managing the document's data; the subclass should do nothing or very little outside of overriding NSDocument's methods. In the NSDocument subclass, I override -[NSDocument makeWindowControllers], and call -[NSDocument addWindowController:] passing in a window controller subclass I create. Initialization code for UI is done in -[NSWindowController windowDidLoad]. As always, a window is associated with one xib and one window controller subclass. NSWindowController also has a useful method for accessing the document via -[NSWindowController document].

Also, the UI can already be loaded before readFromData:ofType:error: has been called as is the case when reverting a document. In this case, one overrides -[NSDocument revertToContentsOfURL:ofType:error:] and tells the window controller(s) to reload their UI from the updated document, if the reverting operation succeeded.

Zorg
  • 978
  • 6
  • 10