Discussion:
Reverting an NSDocument
(too old to reply)
Joe Shimkus
2004-11-14 21:07:05 UTC
Permalink
So I've got a subclass of NSDocument and it receives a
-revertDocumentToSaved: message and loads from the saved file; no
problem. The question is, in trying to maintain a pure MVC organization
how do I inform the controller that the document has changed so it can
update the view since there are attributes that apply to the document
but not to the application as a whole? I think I might be able to do it
with KVC/KVO but I haven't studied that enough to put it into practice
and what was done before KVC/KVO?

Right now I've overridden -revertToSavedFromFile:ofType: in my document
to invoke its super's version and, if the the above returns YES, to
execute the following:

[[self windowControllers]
makeObjectsPerformSelector:@selector(documentReverted)];

which isn't too terrible but does require that the document presuppose
the existence of 'documentReverted' in its window controllers. I can't
locate a notification or delegate method that would inform the
controller.

Any suggestions?
--
PGP Key (DH/DSS): http://www.shimkus.com/public_key.asc
PGP Fingerprint: 89B4 52DA CF10 EE03 02AD 9134 21C6 2A68 CE52 EE1A

Windows has always aspired to be Mac-like without Microsoft ever really
understanding what that even means. - Robert Cringely
James Spencer
2004-11-17 23:42:28 UTC
Permalink
Post by Joe Shimkus
So I've got a subclass of NSDocument and it receives a
-revertDocumentToSaved: message and loads from the saved file; no
problem. The question is, in trying to maintain a pure MVC organization
how do I inform the controller that the document has changed so it can
update the view since there are attributes that apply to the document
but not to the application as a whole? I think I might be able to do it
with KVC/KVO but I haven't studied that enough to put it into practice
and what was done before KVC/KVO?
Right now I've overridden -revertToSavedFromFile:ofType: in my document
to invoke its super's version and, if the the above returns YES, to
[[self windowControllers]
which isn't too terrible but does require that the document presuppose
the existence of 'documentReverted' in its window controllers. I can't
locate a notification or delegate method that would inform the
controller.
Any suggestions?
The more typical method was to keep the separation between the model
and the views complete. Note that the document (which is really a
controller for the model) doesn't really need to know when the model
has changed: the views do which in turn means the window controllers
do, as you note in your version above. With that in mind, my
preference has been to post a notification in my setters in the model
and register my window controllers (or whatever controller is
controlling the views) to receive those notifications. See Bill
Cheeseman's Cocoa Recipes for a really clean example of this. The big
advantage of this is that your model doesn't give a hoot about whether
it is a window controller controlled by the document or not, nor does
it care how many different observers are out there. You can add or
delete observers any time you want.

Spence
--
James P. Spencer
Rochester, MN

"Badges?? We don't need no stinkin badges!"
Loading...