Why aren't the init and windowControllerDidLoadNib: method called when a autosaved document is automatically open ?

StackOverflow https://stackoverflow.com/questions/16347569

Frage

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 ?

War es hilfreich?

Lösung

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:

  1. The NSWindowController method setDocument: sets the restoration class of document windows to the class of the shared NSDocumentController object. The NSWindow object invalidates its restorable state whenever its state changes by sending invalidateRestorableState to itself.
  2. 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.
  3. When the system restarts, Cocoa relaunches the app and sends the restoreWindowWithIdentifier:state:completionHandler: message to the NSApp 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 the restoreWindowWithIdentifier:state:completionHandler: message to the restoration class object [in this case, the document controller's class —Peter], and returns YES.

  4. The restoration class reopens the document and locates its window. Then it invokes the passed-in completion handler with the window as a parameter.
  5. Cocoa sends the restoreStateWithCoder: message to the window, which decodes its restorable state from the passed-in NSCoder 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 the NSApplication object, then to each NSWindow object, then to the NSWindowController object, then to the NSDocument 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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top