I have a NS(Persistent)Document
.
I run my application via Xcode, then create a new document (without any data), and then quit my application.
In this case, I can check that the init
and windowControllerDidLoadNib:
are called
If I re-run my application with Xcode, then the previous document is automatically open.
But, I can check that neither init
nor windowControllerDidLoadNib:
are called.
Why is it so ?
What you're seeing is window restoration. As that document (part of the Document-Based App Programming Guide) puts it:
The document architecture implements the following steps in the window restoration process; the steps correlate to the numbers shown in Figure 5-2:
- The
NSWindowController
methodsetDocument:
sets the restoration class of document windows to the class of the sharedNSDocumentController
object. TheNSWindow
object invalidates its restorable state whenever its state changes by sendinginvalidateRestorableState
to itself.- At the next appropriate time, Cocoa sends the window an
encodeRestorableStateWithCoder:
message, and the window encodes identification and status information into the passed-in encoder.When the system restarts, Cocoa relaunches the app and sends the
restoreWindowWithIdentifier:state:completionHandler:
message to theNSApp
object.Apps can override this method to do any general work needed for window restoration, such as substituting a new restoration class or loading it from a separate bundle.
NSApp
decodes the restoration class for the window, sends therestoreWindowWithIdentifier:state:completionHandler:
message to the restoration class object [in this case, the document controller's class —Peter], and returnsYES
.- The restoration class reopens the document and locates its window. Then it invokes the passed-in completion handler with the window as a parameter.
- Cocoa sends the
restoreStateWithCoder:
message to the window, which decodes its restorable state from the passed-inNSCoder
object and restores the details of its content.[Figure 5-2, and a paragraph explaining that views, other responders, and the document get saved and restored, too]
When the app is relaunched, Cocoa sends the
restoreStateWithCoder:
message to the relevant objects in turn: first to theNSApplication
object, then to eachNSWindow
object, then to theNSWindowController
object, then to theNSDocument
object, and then to each view that has saved state.
The window restoration protocol is used for non-document-related windows, too, but the document machinery handles most of the dirty work for you. If you need to do anything on either side (probably both sides) of window restoration, override encodeRestorableStateWithCoder:
and restoreStateWithCoder:
in your document. The former is where you save transient information like selections, and the latter is where you restore that information in the resurrected document and its window(s).
The presence of coders implies that the document is initialized using initWithCoder:
rather than init
, though this isn't a documented fact (in the context of window restoration) that you should rely upon.