Re: Correct implementation of undoManagerForTextView
Re: Correct implementation of undoManagerForTextView
- Subject: Re: Correct implementation of undoManagerForTextView
- From: Jeremy Dronfield <email@hidden>
- Date: Thu, 4 Mar 2004 15:20:09 +0000
On 4 Mar 2004, at 3:33 am, Dustin Voss wrote:
Secondly, there is an NSWindow delegate method that supplies an undo
manager. If a text view needs an undo manager, it follows the
responder chain, and will get one of these (in order):
* One returned by the NSTextView delegate's -undoManagerForTextView:
That's the one I want. The documentation tells me that "If you want a
text view to have its own NSUndoManager (and not the windows), have
the text views delegate implement undoManagerForTextView:, to return
the NSUndoManager." What it doesn't tell me is how to implement this
method. What do I return?
Well, I don't know why you don't want to use the window's and drawer's
default undo manager, but given that you don't...
This is the setup. I have TextView A in the main window. This view is
always present. I then have TextView B in the drawer, which is
sometimes present. When either view loads new content from file or
closes, I need to check that there are no unsaved changes. If insaved
changes are found, then a dialog is put up asking the user if they want
to save. So far so standard. However, let's say the user edits view A,
then opens the drawer to check something in view B, then closes the
drawer (without having edited it). As the drawer closes, the
application looks for unsaved changes in view B. When it looks, it
finds that there are undos registered (but has no way of knowing that
these changes are in the main window, not the drawer). It puts up an
Unsaved Changes dialog and the user goes "Huh? I didn't edit anything."
Just have the delegate objects instantiate an NSUndoController in the
-init method, and return it.
I assume you mean NSUndoManager. (I got all excited when I read that -
I thought I'd overlooked a whole undo-related controller class.) I
tried putting undoManager = [[NSUndoManager alloc] init]; in the -init
method of each delegate and returning,
- (NSUndoManager *)undoManagerForTextView:(NSTextView *)aTextView
{
return undoManager;
}
This made the entire undo system go haywire - e.g. hitting Undo removed
the entire contents of the view, undos failed to register, or they
registered but didn't enable the Undo menu item. As far as I could
tell, I now had three undo managers (or three conflicting pointers to
the same undo manager) - one for each delegate, plus the default
manager for the main window, which was still trying to manage
everything.
As an alternative, I set -undoManagerForTextView: to return
[[myTextView window] undoManager] in each delegate. This should have
given me two managers - one for the window, one for the drawer - but in
fact it put me right back where I started - i.e. editing View A, then
querying -canUndo for View B returned YES.
I feel like I'm being unbelievably obtuse about this.
-Jeremy
========================================
email@hidden
theLocustFarm.net:
- fractious fiction at
http://freespace.virgin.net/jeremy.dronfield
========================================
_______________________________________________
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.