Setting Action Names

You can use the NSUndoManager method setActionName: to qualify the Undo and Redo command titles in the Edit menu. You pass the string you want appended to “Undo” and “Redo” in the menu items when the current undo group is at the top of the undo and redo stacks. Because the name is applied to the current operation, you should typically set the name at the same time as registering the operation to ensure that the two are kept in sync.

- (void)setBookTitle:(NSString *)newTitle {
    [undoManager registerUndoWithTarget:self
                 object:[book title]];
    [book setTitle:newTitle];
    [undoManager setActionName:@"Title Change"];

Consider, for example, a graphics application that allows users to add a circle, fill it with a color, and delete it. With setActionName:, you could set the name of each action to “Add Circle,” “Fill,” and “Delete.” After each action, the Undo menu item title is set to “Undo Add Circle,” “Undo Fill,” and “Undo Delete” respectively.

NSUndoManager automatically localizes the “Undo” and “Redo” portion of the command titles, but merely appends the action name to them. You should localize the action names yourself. If you want to further customize how these titles are localized, you can create a subclass of NSUndoManager and override undoMenuTitleForUndoActionName: and redoMenuTitleForUndoActionName:.