16

In Cocoa Applications, you often see a little red dot in the window's close button when you have unsaved data; TextEdit is a good example of this. I've pored through the Cocoa documentation but I can't find a way to programmatically set this state. I'm sure there's some really easy way to do it, but obviously I'm missing something.

3 Answers 3

18

To set it programmatically, you can use the -setDocumentEdited: method of NSWindow. If you are writing a Document-based app, NSDocumentManager should automatically detect when there are unsaved changes to the NSUndoManager associated with the current NSDocument.

14

Depends on what kind of application you're building. If it's NSDocument based, use NSDocument's updateChangeCount: method. If you've just got an NSWindowController, use setDocumentEdited:. NSWindow has a setDocumentEdited: method if all you've got is the NSWindow.

1
  • And of course you can test the state when it's time to close or whenever: in your window controller if self.window!.isDocumentEdited {.... Saves defining you own variable.
    – AlexT
    May 7, 2019 at 10:21
9

Also, in 10.6, the 'setDocumentEdited' marks the application as dirty, and so can't be fast-killed. If you don't call this, and set the flag in the Info.plist (see What's New in 10.6)

<key>NSSupportsSuddenTermination</key>
<string>YES</string>

That way, if your app is running (but isn't dirty) then Mac OS X can simply kill it, rather than invoking polite shutdown requests. If your document(s) are marked dirty (or the window is) then it'll go through the normal app shutdown process to shut it down.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.