Undo action names for Core Data changes

末鹿安然 提交于 2019-12-21 17:28:56

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!