9

I'm working on using encodeRestorableState/restoreStateWithCoder to save and restore the state of a window. (I'm doing this manually because I want to explicitly save my window state in the document and not just rely on the OS restoring its state as part of restoring the application state)

The problem I'm having is that when it restores, an error is logged in the console that it can't restore the first responder because the view in question has its window set to nil.

I tried calling makeFirstResponder(nil) before encoding, but then the window is its own first responder and an exception is thrown because it can't encode itself.

I'm doing this from my window controller's windowDidLoad. Doing it in awakeFromNib didn't make a difference.

So is there a way I can either prevent the window from saving/restoring the first responder, or ensure that the first responder view is installed in the window first?

Uncommon
  • 3,323
  • 2
  • 18
  • 36
  • From where and how do you call `encodeRestorableStateWithCoder:` and `restoreStateWithCoder:`? – Willeke Feb 12 '17 at 11:32
  • I call `encodeRestorableStateWithCoder` in the window controller's `windowWillClose:`, saving the result to a file manually. Then in `windowDidLoad` I read the file back in and call `restoreStateWithCoder`. I have also tried restoring in `awakeFromNib` instead, with the same result. Aside from the first responder issue it's working fine. – Uncommon Feb 13 '17 at 15:24
  • check this http://stackoverflow.com/questions/5266634/firstresponder-in-nsviewcontroller. Let me know for questions. – Luke Ahn Feb 16 '17 at 17:03
  • @LukeAhn That doesn't relate to encoding/decoding windows. – Uncommon Feb 16 '17 at 17:05

0 Answers0