NSViewControllers and NSViews are not getting restoration API calls
Asked Answered
S

1

9

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
  }
}
Solly answered 2/2, 2017 at 18:47 Comment(7)
Are you calling invalidateRestorableState? It's barely mentioned in Apple's description of the restoration machinery and not mentioned at all in the description of encodeRestorableState so it's easy to overlook (or at least it was for me for the last half hour).Convict
Same problem here. Did you finally figure out what the problem was?Cytoplasm
same problem hereJovia
Same problem. Wasted hours already.Danseuse
@Cytoplasm I posted an answer. Let me know if it still does not work!Prepositor
@CharltonProvatas I posted an answer.Prepositor
@ElnurAbdurrakhimov I posted an answer.Prepositor
P
3

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.

Prepositor answered 4/3, 2019 at 8:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.