Re: NSDocument's isDocumentEdited
Re: NSDocument's isDocumentEdited
- Subject: Re: NSDocument's isDocumentEdited
- From: "John Love" <email@hidden>
- Date: Wed, 23 Jul 2008 08:55:39 -0400
In the following, I have expanded on what I believe I have learned Grayson's
response. (Any errors are mine, not Grayson's)
Apple's comments within their "NSDocument.h" state that with:
- (void)updateChangeCount:(NSDocumentChangeType)change
"Record the fact that a change affecting the value returned by
isDocumentEdited has occurred. ... Your application only needs to invoke
this method explicitly if it is not taking advantage of NSDocument's
built-in undo support. The default implementation of this method also sends
all of the document's window controllers setDocumentEdited: messages when
appropriate."
I have deleted my override of NSDocument's –isDocumentEdited within
MyDocument.m and recorded the fact that MyDocument may have been edited via
calling my own setFileEdited whenever I see a possibility of editing
MyDocument.
- (void) setFileEdited {
BOOL isEdited;
if (whateverParm1 || whateverParm2)
isEdited = TRUE
else
isEdited = FALSE;
if (isEdited) [self updateChangeCount:NSChangeDone];
else [self updateChangeCount:NSChangeCleared];
}
Apple states that NSChangeDone is the value to pass to updateChangeCount:
to indicate that a single change has been done .. NSChangeCleared is the
value to pass to indicate that the document has been synchronized with its
file or file package.
Based on Apple's comments at the top of this entry and in other sources,
NSDocument's isDocumentEdited is called when updateChangeCount: records a
change. Window's "dirty" flag is automatically set/cleared by Cocoa
following its call to –isDocumentEdited.
So, I do not have to override –isDocumentEdited for this purpose; rather I
just have to call updateChangeCount when appropriate, and Cocoa takes care
of the rest.
For those who have a separate Controller which needs to address
setFileEdited issues, use standard a delegate call, coupled with a informal
protocol.
When MyDocument initially calls MyController, MyDocument passes self as the
delegate and within MyController, self is stored in MyController's
mDocDelegate which is typed as a generic id pointer.
setFileEdited: would now be in MyController.m, and its setFileEdited:
would have:
if (isEdited) [mDocDelegate updateChangeCount:NSChangeDone];
else [mDocDelegate updateChangeCount:NSChangeCleared];
An informal protocol is added to the mix because mDocDelegate, as just
stated, is typed as a generic id pointer and the real identity (a pointer to
MyDocument) is finally resolved only at run-time.
So, add the following to MyController.h:
@interface NSObject (MyController)
- (void) updateChangeCount:(NSDocumentChangeType)change;
@end
In summary, I have outlined two scenarios .. test for document editing
within your MyDocument or within a separate Controller. In either case, you
just need to call updateChangeCount, rather than mess with overriding
NSDocument's –isDocumentEdited.
John Love
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden