Re: Subclassing NSWindowController
Re: Subclassing NSWindowController
- Subject: Re: Subclassing NSWindowController
- From: Mike Ferris <email@hidden>
- Date: Thu, 5 Dec 2002 09:51:28 -0800
One kind of non-obvious thing about NSDocument and NSWindowController
is that you have to be prepared for a a couple situations:
- Loading a document without UI
- Loading a document with UI
- UI getting created once a document has loaded its data
This can especially trip people up with text documents because
NSTextView makes it so easy to simply deal with everything at the view
level.
What you want to do, though, especially when using separate NSDocument
and NSWindowController objects, is be sure to keep model and view
separated. In the case of text, this boils down to the content vs. the
NSTextView. The document should own the content, the controller should
own the view. The easiest way is to make the NSDocument own an
NSTextStorage which is the "model" for the text content. Part of
hooking your NSWindowController up to its document (in an override of
-setDocument: perhaps) should be adding the -layoutManager of the
controller's NSTextView to the text storage owned by the document.
Here's one possible implementation strategy:
- document has-a NSTextStorage
- init method of your document creates an NSTextStorage
- load or read method of your document loads or reads into the
NSTextStorage
- window controller has-a
NSTextView-NSTextContainer-NSLayoutManager chain of objects (but no
NSTextStorage).
- window controller creates these objects programmatically, probably
- window controller's setDocument: makes sure the NSLayoutManager
gets connected to the NSTextStorage of the new document (using
NSTextStorage's addLayoutManager:).
This strategy handles the various permutations including a document
that loads before its UI is created, or after. It handles the Revert
case, etc...
Mike Ferris
Begin forwarded message:
From: Chaffinch <email@hidden>
Date: Thu Dec 5, 2002 2:16:52 AM US/Pacific
To: email@hidden
Subject: Subclassing NSWindowController
Hi,
I am writing a text editor as a way of learning Cocoa. I want to
customise
the window title to display the full path name. I have managed to
implement
this by subclassing NSWindowController and overriding the
windowTitleForDocumentDisplayName method. So far so good. I have also
overridden MyDocument's makeWindowControllers method to create the
window
controller and call addWindowController. So far so good.
Only now, opening or saving a document no longer works??
I have read through the Apple docs, and have made the NSTextView member
(previously owned by the document) a member of the subclassed
NSWindowController. And overridden windowDidLoad in an attempt to load
the
document.
Am I correct in thinking that I still need the loadDataRepresentation
and
dataRepresentaionOfType methods in the document class to load and save
the
document but that the actual displaying is now done by the windo
controller?
Have I missed something?
I have looked on stepwise, cocoadevcentral, apple etc for an example of
subclassing NSWindowController but come up blank. Anyone know of one?
The closest I came was on oreilly.com, ImageApp does this, so I have
got some
ideas from there but still no success. One thing hey suggest is to
change the
File's Owner in Interface Builder, but following the instructions does
not
work on my version. I am guessing that the tutorial was written for an
earlier version of the dev tools. I am using OSX 10.2.2.
Any ideas.
Thanks very much for your time.
=====
Regards
Chaffinch
----------------------------------------------
Everything you'll ever need on one web page
from News and Sport to Email and Music Charts
http://uk.my.yahoo.com
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.