Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSViewControllers and NSViews are not getting restoration API calls

in Apple's documentation they say that in case of document-based apps you should get restoration calls (restoreStateWithCoder: and encodeRestorableStateWithCoder:) to NSApplication, NSWindow, NSWindowController and then the whole responder chain (here).

I want to implement this, but I'm getting the restoration calls only in NSWindowController/NSDocument subclass, not the NSViews or NSViewControllers.

I created a new document-based app to test this (here), but I get the restoration calls only in the NSDocument subclass but not the NSViewController or NSView.

Code from the test project:

NSDocument subclass (restoration works):

class Document: NSDocument {

  override func restoreState(with coder: NSCoder) {
    super.restoreState(with: coder)
    // Gets called after reopening the app
  }

  override func encodeRestorableState(with coder: NSCoder) {
    super.encodeRestorableState(with: coder)
    // Gets called when leaving the window for the first time
  }


}

NSViewController subclass (restoration doesn't work):

class ViewController: NSViewController {

  override func restoreState(with coder: NSCoder) {
    super.restoreState(with: coder)
    // Not called
  }

  override func encodeRestorableState(with coder: NSCoder) {
    super.encodeRestorableState(with: coder)
    // Not called
  }

}

NSView subclass (restoration doesn't work):

class MyView: NSView {

  override func restoreState(with coder: NSCoder) {
    super.restoreState(with: coder)
    // Not called
  }

  override func encodeRestorableState(with coder: NSCoder) {
    super.encodeRestorableState(with: coder)
    // Not called
  }
}
like image 648
Pavel Smejkal Avatar asked Feb 02 '17 18:02

Pavel Smejkal


1 Answers

I just ran into the same problem. It seems the state preservation system uses the identifier property to determine whether to encode/restore the state of an object.

According to the documentation, the identifier is automatically populated when an object is loaded from a NIB file. However, it needs to be set manually if an object is created programmatically.

like image 155
fumoboy007 Avatar answered Oct 19 '22 16:10

fumoboy007