The MacOS application template in a newer version of Xcode defaults to using storyboards. To implement custom Undo and Redo operations it is necessary to implement to have your NSView implement NSWindowDelegate
.
Create an NSUndoManager
for the view:
NSUndoManager* undoManager;
Assign the delegate programmatically within initWithFrame
and set your view as the first responder:
NSWindow* mainWindow = [[[NSApplication sharedApplication] windows] objectAtIndex:0];
mainWindow.delegate = self;
undoManager = [[NSUndoManager alloc] init];
[self becomeFirstResponder];
Then in the method where the undoable action takes place
[[undoManager prepareWithInvocationTarget:self] undoAction];
if(![undoManager isUndoing]) {
[undoManager setActionName:@"Select"];
}
Where undoAction
is the method on your view.
For a working example see this LifeView.m in my Game of Life cocoa app.