Marking a Document Window as Edited
An application should properly handle the closing of a document
window by setting the
docEdited
flag, an instance variable declared in the
NSWindow class. The
docEdited flag determines whether an on-screen
window’s close button (in the upper-left corner of
the window) is a solid red disk (docEdited=NO) or
has a dot in its center (docEdited=YES). The two possibilities are shown in Figure 13-6.

Figure 13-6. MathPaper windows without (left) and with (right) dot in close button
In addition to providing feedback to the user in the close button,
the docEdited flag is used by Cocoa programs to
determine whether a document window can safely be closed without any
loss of data. Obviously, the docEdited flag should
be set when the text inside the MathPaper page has been edited and
unset when it has been saved or newly opened.
The Cocoa multiple-document architecture automates much of the
maintenance of the docEdited flag with a second
flag, called the change
count
. The framework knows to reset a
document’s change count when it is saved. All you
need to do is to set the change count when the document is first
“dirtied.”[34]
Insert the statement shown here in bold into the textDidChange: method in
PaperController.m:- (void)textDidChange:(NSNotification *)notification { NSString *str = [ [ [self window] currentEvent] characters]; [[self document] updateChangeCount:NSChangeDone]; ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access