iamjustaprogrammer
iamjustaprogrammer

Reputation: 1664

In a document-based app how do you prevent data loss when the user accidentally pressed cmd+w rather than cmd+s?

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?

Upvotes: 1

Views: 54

Answers (1)

andyvn22
andyvn22

Reputation: 14824

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.

Upvotes: 1

Related Questions