0
votes

I have an NSDocument-based app where a window for the document may have child windows that need to be queried for changes before the document can close. So I tried to intercept the closing of the document window to do this, but I can't find a reliable way.

I tried to use -[Document canCloseDocumentWithDelegate:shouldCloseSelector:contextInfo:] for this, but apparenty this is only called when the document has changes (signalled by -updateChangeCount). If there are no changes, this never gets called.

My other attempt to use the view controller's viewWillDisappear to update the document failed too, as this is too late to register any changes with the document, so they do not get saved (canCloseDocumentWithDelegate:... has already been called at this point.)

What would be the best way to solve this?

1
Why don't you update the change count? - Willeke
Ah, that's complicated to explain. I wanted to keep the change in the child window independent from the main document, so the user could decide not so save the changes in the child to the main document. But I'll likely change that to go with a more traditional approach. - fbitterlich
Did you ever manage to solve this? I have the same problem. I have a text field that is usually committed only after the user presses Return, but when the document closes, I need to save those changes with the document. - Mark
I went with the suggestion of @Amin Negm-Awad - I changed the structure so that the main window knows of any changes of the "child" windows. After thinking it over, it seemed logical. - fbitterlich

1 Answers

2
votes

Well, I'd say that if the child windows contains changes that have to asked for before the document is closed, the document has changes. And this should be reflected in the (main) document window. However, …

Likely -shouldCloseWindow: should be called, if the document does not contain changes and therefore the document system does not ask for saving itself.