问题
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".
回答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)];
}
来源:https://stackoverflow.com/questions/5696309/undo-action-names-for-core-data-changes