On May 13, 2010, at 21:20, Richard Somers wrote:

> Consider a Core Data document based application that uses a 
> NSArrayController. When the selected objects are removed or deleted and then 
> the user does an undo the selected objects are restored but the selection is 
> not. I have a custom view object.
> 
> I would like the state of the selection to also be restored with an undo 
> similar to what happens when working with the Cocoa text system. When text is 
> selected, then deleted, and then the user does an undo, the restored text is 
> selected.
> 
> Should I try to use the persistent document undo manager, create an undo 
> manager in the custom NSArrayController subclass, create an undo manager in 
> the custom view, or create an undo manager in one of my other classes?

This is a little bit harder than it seems. You certainly want to have the 
document undo manager keep track of the selection changes, in order to stay in 
sync with the really undoable changes. So:

1. You need to add a transient property for the selection to your managed data 
model. (Or non-transient, if you want the selection to persist when the 
document is re-opened.)

2. You need to keep track of the changes to the selection by modifying your 
Core Data selection property whenever the selection changes. However, you don't 
want these to be recorded as undoable actions (most likely, although there are 
scenarios -- think of Photoshop -- where selection changes are undoable), so 
you have to disable the undo manager temporarily around such changes. (Don't 
forget to invoke processPendingChanges before disabling the undo manager, and 
again before enabling it.)

3. You can't just track the selection state at the time of the undoable action, 
because the selection you ultimately want is different depending on whether 
you're undoing or redoing. (Convincing yourself that this is an issue is left 
as an exercise for the reader.) I think the easiest way to do this is to create 
an undo group for each action that needs to restore the selection, with 2 
actions in the group: the first one changes all the non-selection properties to 
their new values; the second changes the selection property to the post-change 
selection. (The latter changes the selection without disabling the undo 
manager, of course.) I *think* that gives the right selection depending whether 
you're undoing or redoing. (But I haven't actually tried this approach. The 
last time I had to deal with this problem in a Core Data environment, I think I 
did something clunkier.)

_______________________________________________

Cocoa-dev mailing list ([email protected])

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to