Lost with Window resume feature, autosave in user preferences and save in model for NSDocument
Asked Answered
W

1

11

I started a little document based application with a NSOutlineView on the main window. I saved my model in files using NSCoding protocol in the model classes and everything is fine.

Next I wanted to save the user interface (window size and position, expanded items in the outline view, selection, ...) and i found the Resume feature added to Lion.

So I tried to implement it, I added an autosave name to the main window and the outline view, set the autosaveExpandedItems property and implemented the outlineView:itemForPersistentObject: and outlineView:persistentObjectForItem: method in the data source of outline view.

That works but when I create / open an another file then close it, the expanded items state of the others files are set with the state of the closed file.

It's like the app saves only one window for all documents and not one window per document.

I am a bit lost with autosave and resume, is it the same feature or two features completely different ?

Can I use it to save window state or need I to save it in my model ?

Thank you

Wellesley answered 19/11, 2012 at 16:28 Comment(0)
H
1

I realize this is about 3 years too late, but maybe this will help anyone who ends up here.

I am by no means a Cocoa expert or professional mac developer yet (still learning), so I'm afraid I can only offer a little bit of insight (having been working on a document-based app myself).

It might be the case that implementing the saving state data on a per window and per control basis is simply tying it to a single instance (since it's all pre-loaded in xibs and tied to a single autosave name), but I could be wrong about this. Anyone who's more knowledgable is free to correct me.

Perhaps try and save your state information at the NSDocument level instead of for the window, and then implement window(window: NSWindow, willEncodeRestorableState state: NSCoder) and window(window: NSWindow, didDecodeRestorableState state: NSCoder) to pull from your document data.

You can, of course, easily access the document for the current window: view.window?.windowController?.document as? YourNSDocumentSubclass

This is more or less what I'm doing, and it seems to be promising so far. Other architectural gurus may offer more insight.

Hamilton answered 4/5, 2016 at 4:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.