Undo action names for Core Data changes
Asked Answered
L

1

4

I'm looking for a built-in (or easy-to-implement) way to get Core Data undo/redo action names to read like "Undo edit First Name", rather than simply "Undo" or "Redo" as they do by default.

I have a Core Data application, and am using its NSUndoManager as my window's undo manager. It works great, but when a user makes a change to a field (inline from an NSTableView), the Undo menu item's title doesn't reflect which field changed.

Initial searching led me to the same question posted on Apple Mailing Lists in January 2007. The only answer ever posted responds with "Search for 'Model.strings' in the Core Data Programming Guide." I didn't have a Strings file for my Model, so I created one (localized in my en.lproj directory and with UTF-16 encoding), but this made no difference.

I followed instructions from Apple's tutorial (adapted slightly since my app is not Document-based), and the Core Data documentation, but my menu titles still read "Undo" and "Redo".

Lonna answered 17/4, 2011 at 20:43 Comment(0)
C
1

In your NSManagedObject subclass add

-(void)setValue:(id)value forKey:(NSString *)key
{
    NSUndoManager * aUM = [[self managedObjectContext] undoManager];
    [super setValue:value forKey:key];
    if ([aUM isUndoRegistrationEnabled])
        [aUM setActionName:NSLocalizedString(key,nil)];
}
Coleville answered 23/4, 2011 at 9:40 Comment(3)
The NSLocalizedString call in your sample won't work though, since that won't look up against the Model.strings file. How would you look up a field's localized name at runtime?Lonna
I posted the above as a separate question: https://mcmap.net/q/833108/-localized-core-data-names/105717Lonna
I also modified your if statement accordingly: if ([aUM isUndoRegistrationEnabled] && ![aUM isUndoing] && ![aUM isRedoing]) to avoid duplicate entries every time an undo or redo is performed.Lonna

© 2022 - 2024 — McMap. All rights reserved.