Multiple undo managers for a text view
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 respo开发者_开发知识库nder (that is, the focus of keyboard actions). Once the insertion point leaves the field or cell, prior operations cannot be undone.
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.)
精彩评论