Chapter 39. Undo

The ability to undo the most recent action is familiar from Mac OS X. The idea is that, provided the user realizes soon enough that a mistake has been made, that mistake can be reversed. Typically, a Mac application will maintain an internal stack of undoable actions; choosing Edit → Undo or pressing Command-Z will reverse the action at the top of the stack, and will also make that action available for Redo.

A pervasive, extensive implementation of Undo makes sense on Mac OS X, especially when real-life objects are involved. For example, a window may represent an actual document, a file on disk; it would be terrible if every word typed or pasted or cut represented a permanent, irreversible change to that document. Given the transient, visual nature of the iOS interface, however, and the sorts of thing for which iOS apps are typically intended, users do not generally expect Undo at all.

Nevertheless, some iOS apps may benefit from at least a limited version of this facility. Not every action needs to be undoable, and the ability to undo needn’t persist for very long. And limited Undo is not difficult to implement. Some built-in views — in particular, those that involve text entry, UITextField and UITextView (Chapter 23) — implement Undo already. And you can add it in other areas of your app.

The Undo Manager

Undo is provided through an instance of NSUndoManager, which basically just maintains a stack of undoable actions, along with a secondary stack of redoable actions. ...

Get Programming iOS 5, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.