Re: Handling Undo/Redo in KVC To-Many Accessors
Re: Handling Undo/Redo in KVC To-Many Accessors
- Subject: Re: Handling Undo/Redo in KVC To-Many Accessors
- From: Chris Giordano <email@hidden>
- Date: Mon, 10 May 2004 17:43:33 -0400
Sean,
On May 10, 2004, at 4:40 PM, Sean Todd wrote:
Cc: email@hidden
From: Chris Giordano <email@hidden>
Subject: Re: Handling Undo/Redo in KVC To-Many Accessors
Date: Mon, 10 May 2004 10:15:05 -0400
To: Sean Todd <email@hidden>
<snip>
From some testing, it appears that it's crashing because it's
attempting to do something (probably restore) the selection in the
table, but since the selected indexes now extend past the end of your
array, it's complaining.
There are two things that I could see doing to fix this. The first is
probably easiest -- assuming that it works in all of your cases. That
would be to simply remove the selection when you delete. You could do
something to attempt to restore it (i.e., scan the previously selected
indexes/objects and reselect then once you're done with your delete).
Thanks for the feedback Chris.
No problem. Glad to help.
Yes, adding one line of code is the simplest workaround:
- (void)removeObjectFromToDosAtIndex:(unsigned int)index
{
NSUndoManager* um = [self undoManager];
id item = [self objectInToDosAtIndex:index];
[[um prepareWithInvocationTarget:self]
insertObject:item inToDosAtIndex:index];
[toDoTableView deselectAll:self];
[toDos removeObjectAtIndex:index];
}
Of course, this is just doing programatically what was being done
manually by selecting another item in the table view before invoking
the undo command. However, I am still left wondering if this should be
needed in the first place (i.e. is this a bug with NSArrayController)?
After all, NSTableViews are often bound to array(like) objects via
NSArrayControllers. So I would expect the framework to handle any
paired calling of insertObject:in<key>AtIndex: followed by
removeObjectFrom<key>AtIndex: without getting confused.
I think (or more accurately, am guessing that) the primary problem is
that this change to the model is happening in a way that isn't entirely
"compatible" with the controller. That is, the controller has some
code that adjusts the selection when an object is removed, and this
isn't triggered by changing the model. (It will be a few more readings
through the KVC and KVO documentation before I have it internalized.)
You could try using the array controller's
insertObject:atArrangedObjectIndex: and
removeObjectAtArrangedObjectIndex: methods rather than the model's
insertObject:inToDosAtIndex: and removeObjectAtIndex: for the undo
actions. This should still trigger the model changes, but should also
cause to happen all the other magic that makes array controllers so
convenient. I don't think I tried that when I was trying to replicate
your problem in my code since it really wouldn't fit with my code, but
it might work, would involve changing only two lines (other than the
one addition), and it's possible you wouldn't have to lose the
selection.
Just a thought.
chris
_______________________________________________
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.