objective-ccocoansstringnsmenunsundomanager

NSUndoManager setActionName: doesn't change menu text


I've been implimenting undo features for my cocoa app (OS 10.9), and I have successfully implemented an instance of an undo feature using NSUndoManager. However, I cannot get the "Undo" text to change under the edit menu! I am using the setActionName method as detailed in Apple's docs. My relevant method:

- (void)FinishEditingName {

[[self UndoManager] registerUndoWithTarget:[self TestObject] selector:@selector(setName:) object:[NSString stringWithFormat:@"Testing Undo!"]];
[[self UndoManager] setActionName:@"Edit Name"];

[[self TestObject] setName:[NSString stringWithFormat:@"New Name"]];

NSLog([NSString stringWithFormat:@"%@",[[self UndoManager] undoActionName]]);

}

Again, the undo works perfectly, i.e. [[self TestObject] Name] is set "New Name" on calling this method, selecting Edit -> Undo changes this to "Testing Undo!". However, the Edit menu's first entry should change to the text "Undo Edit Name", but it doesn't, it just stays as "Undo". As you can see, I've confirmed that the text should change by spitting it out with NSLog(), which works just fine (i.e. it gives "Edit Name"). What am am missing here? Do I need to link the NSUndoManager to the NSMenu somehow? If so, this is missing from the docs!

Thanks in advance!


Solution

  • The problem is I am not using NSDocument, in which case the link between the menu and the NSUndoManager is not set up automatically - you need to handle it yourself. This appears to be in the NSDocument docs, but not in the NSUndoManager docs, where, IMO, it should be noted. Thanks for trying to help!