Re: NSUndoManager, NSTableView with bindings
Re: NSUndoManager, NSTableView with bindings
- Subject: Re: NSUndoManager, NSTableView with bindings
- From: Graham Cox <email@hidden>
- Date: Mon, 17 Feb 2014 12:13:40 +1100
On 17 Feb 2014, at 3:34 am, Trygve Inda <email@hidden> wrote:
> How is the best way to do this?
>
> Since the table uses bindings, the setting of the property is handed by the
> binding system.
One way to handle this is illustrated in Hillegasse's classic book on Cocoa. I'm looking at the 2nd edition, not sure if the 3rd has the same scheme.
If your undoable properties are all properties, i.e. they have a getter and a setter, you can use KVO to observe the undoable properties in a handler object that acts solely as a go-between between NSUndoManager and the model class. It observes property changes using KVO and uses the 'will change' info to set up Undo. The target is the handler object and every undo gets the same method. The parameter to the method is the KVO change dictionary. When NSUndoManager undoes, it calls this method on the handler with all of the original observed KVO info. This allows the handler object to simply call the relevant property setter with the original value.
The advantage of this approach is that a) no custom getters/setters are needed, it just uses KVO. b) you can set up observation only only those properties that you want to be undoable, others can be ignored if you want. c) Undo automatically updates, through bindings, any UI related to the undone property, d) the data model itself doesn't need to know anything about the undo manager.
The only real drawback of this approach is that because Undo in this case is sort of "hanging off the side" of your data model, it doesn't get a simple UI-related string that it can use to label each Undo action. That's a fairly minor problem though, easily solved with some additional work to map property names to a user-readable and localisable string.
--Graham
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden