Question

I've implemented an undo function in my paint application. The idea is that the undo sets the layer to draw from an array of saved layers. This method is called right before the view is added to for drawing. Note that layerArray is a C array.

-(void)updateLayerUndo
{
CGLayerRef undoLayer = CGLayerCreateWithContext([self.theCanvas viewContext], self.theCanvas.bounds.size, nil);

layerArray[undoCount] = undoLayer;
undoCount += 1;
[[self.undoer prepareWithInvocationTarget:self]performUndoOrRedoWithValueForLayer:layerArray[undoCount - 1]];

}

When the user undoes, it calls the registered method, naturally:

-(void)performUndoOrRedoWithValueForLayer:(CGLayerRef)aLayer
{
undoCount -= 1;
self.theCanvas.myLayer = aLayer;
[[NSNotificationCenter defaultCenter]postNotificationName:@"UndoOrRedoPerformed" object:nil];
[self.theCanvas setNeedsDisplay:YES];
}

The view then does its drawing method, which really just draws the myLayer in the view's context. However, when the user undoes, the view clears out completely instead of incrementally. I feel like I'm missing something, or maybe I don't understand NSUndoManager. Oh, and the methods I showed above are both in an NSDocument subclass, and theCanvas is an instance of an NSView subclass.

Was it helpful?

Solution

I figured it out. The confusion I was having is I was unclear how to set redo, so I misinterpreted what the documentation meant about the undo manager and redoes. I now know that to register a redo, I have to register a undo that is called while the manager is undoing.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top