views:

95

answers:

1

Hi,

I've a text view that gets its content from an attributed string stored in a model object. I list several of these model objects in a drawer and when the user clicks on one the text view swaps its content.

I now need to also swap the undo manager for the text view. I initialise an undo manager on my model object and use undoManagerForTextView to return it to the text view, but something's not quite right.

Strategically placed logging statements show me that everything's working as planned: on startup a new model object is initialised correctly and a non-null undo manager is always pulled by the text view. But when it comes to actually doing undo, I just can't get the behaviour I want.

I open a window, type something and press cmd+z, and undo works. I open a window, type something, select a new model on the table, type something, go back to the first model and try to undo and all I get is a beep.

Something on the documentation made me raise an eyebrow, as it would mean that I can't have undo with several model objects:

The default undo and redo behavior applies to text fields and text in cells as long as the field or cell is the first responder (that is, the focus of keyboard actions). Once the insertion point leaves the field or cell, prior operations cannot be undone.

+2  A: 

You may want to subclass NSUndoManager and override removeAllActionsWithTarget: and removeAllActions, at least to log them, possibly to make them no-ops.

Don't forget to instantiate your subclass instead of the original NSUndoManager. (Easy to forget, and some readers may not realize that they need to do this.)

Peter Hosey
I think I'm going to go with Core Data. If I turn my model object into a managed entity I'll get undo for free.
Rui Pacheco
Your method seems to work just fine, no need to add core data to the app. Thanks!
Rui Pacheco