How to use NSUndoManager removeAllActionsForTarget:?
How to use NSUndoManager removeAllActionsForTarget:?
- Subject: How to use NSUndoManager removeAllActionsForTarget:?
- From: Bill Cheeseman <email@hidden>
- Date: Mon, 15 Apr 2002 16:38:44 -0400
I thought I understood how to remove an undo menu item from the Edit menu
after the associated object is deleted. However, it isn't working. It seems
unlikely that this would be a bug in Cocoa
I set up the undo manager correctly, as far as I know. The object is a
record in an array, and I eventually remove it by calling NSArray's
removeObject: method. While it is in the array, my window controller's
action method calls the model object's set accessor to change the value of
the model object's instance variable, then the action method calls the undo
manager's actionName: method to give the undo menu item an appropriate name.
My model object's set accessor registers the change to its instance variable
with the undo manager, using invocation and setting the target to self, then
changes its instance variable. All this is very standard, and multiple undo
and redo work correctly.
Eventually, the window controller deletes the model object by removing it
from the array. The array automatically releases the model object, resulting
in a call to the model object's dealloc method. The dealloc method calls
removeAllActionsWithTarget:self. This is how I understand the documentation
to say it should be done.
But the Undo menu item is still there when I open the Edit menu!
I have used the debugger to verify that the dealloc method is called and
that it executes the removeAllActionsWithTarget: method. I have verified
that the undo action's target is indeed the selfsame model object, by
examining the undo manager's _undoStack private instance variable. Indeed, I
have even verified that removeAllActionsWithTarget: succeeded in removing
the action from the undo stack. As expected, nothing happens when I choose
Edit -> Undo to attempt to execute the spurious menu item, because its undo
action is gone. But the spurious menu item is still there.
If I call removeAllActions in the dealloc method, instead of
removeAllActionsWithTarget:, the menu item goes away as desired.
What am I overlooking? Is this a case of menu validation not working as
expected? Or am I supposed to do something more than just remove the action?
From the documentation, I would expect all of this to be automatic,
especially since removeAllActions works.
For the technically minded, I believe a bug is at work here. Can somebody
verify this? The undo stack doesn't look right in the debugger after calling
removeAllActionsWithTarget:, but I don't know how to interpret what I'm
seeing.
A. When I enter the dealloc method, the undo stack looks like this:
0 - endUndoGrouping
1 - target .... [this is my target model object]
2 - beginUndoGrouping
B. After I call removeAllActionsWithTarget:, the undo stack looks like this:
0 - endUndoGrouping
1 - beginUndoGrouping
In other words, there is an empty undo group sitting there, which I didn't
think was supposed to happen.
C. After I call removeAllActions, instead, the undo stack is empty.
If I understand correctly, cases B and C should be identical, since I
haven't put any other undo actions on the stack, but they aren't identical.
It doesn't seem like the endUndoGrouping and beginUndoGrouping items should
still be on the stack after removing the action. In fact, canUndo still
returns true in case B, even though there aren't any real actions on the
stack, apparently because the empty undo group is still there.
Is removeAllActionsWithTarget: failing to remove the group delimiters when
it should? Is this a bug, or am I responsible for getting rid of them (and
if so, how?)?
--
Bill Cheeseman - email@hidden
Quechee Software, Quechee, Vermont, USA
http://www.quecheesoftware.com
The AppleScript Sourcebook -
http://www.AppleScriptSourcebook.com
Vermont Recipes -
http://www.stepwise.com/Articles/VermontRecipes
Croquet Club of Vermont -
http://members.valley.net/croquetvermont
_______________________________________________
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.