Search code examples
objective-ccocoacore-datansarraycontrollernsundomanager

How to name Undo menu entries for Core Data add/remove items via bindings and NSArrayController?


I have a NSTableView populated by a Core Data entity and Add Item / Remove Item buttons all wired with a NSArrayController and bindings in Interface Builder.

The Undo/Redo menu items can undo or redo the add / remove item actions.

But the menu entries are called only „Undo“ resp. „Redo“.
How can i name them like „Undo Add Item“, „Undo Remove Item“, etc.

(I am aware, something similar was asked before, but the accepted answers are either a single, now rotten link or the advice to subclass NSManagedObject and override a method that Apples documentation says about: "Important: You must not override this method.“)


Solution

  • Add a subclass of NSArrayController as a file in your project. In the xib, in the Identity Inspector of the array controller, change the Class from NSArrayController to your new subclass.

    Override the - newObject method.

    - (id)newObject
    {
        id newObj = [super newObject];
    
        NSUndoManager *undoManager = [[[NSApp delegate] window] undoManager];
        [undoManager setActionName:@"Add Item"];
    
        return newObj;
    }
    

    Also the - remove:sender method.

    - (void)remove:(id)sender
    {
        [super remove:sender];
    
        NSUndoManager *undoManager = [[[NSApp delegate] window] undoManager];
        [undoManager setActionName:@"Remove Item"];
    }