1

My document-based app immediately terminates the document upon cmd+w and it is not saved. How can I react to this and either prompt for saving the document or automatically save it?

iamjustaprogrammer
  • 1,634
  • 2
  • 17
  • 34

1 Answers1

1

Use the NSDocument method updateChangeCount(_:) to mark your document as edited, and Cocoa will take care of the rest!

Furthermore, if you use NSUndoManager for all changes, it updates the change count for you!

Most of the time, you should look for these kinds of "hooks" into existing Cocoa functionality, so that your app behaves exactly the same way as other OS X apps, and so that you inherit new features when new OS versions are released.

andyvn22
  • 14,696
  • 1
  • 52
  • 74
  • Tried [self updateChangeCount:NSChangeUndone]; in readFromData... with no effect. Doc still closes instantly and all data lost. – iamjustaprogrammer Sep 11 '15 at 22:18
  • That's because `NSChangeUndone` is for telling the document that you've UNdone a change--use `NSChangeDone` instead. – andyvn22 Sep 12 '15 at 00:32