Difficulty implementing NSUndoManager repeat function

I am trying to implement NSUndoManagerin my iOS application. I got the undo functionality, but not the reusable part. I'm new to iOS development, and this is the first time I've used it NSUndoManager, so it is probably something trivial.

My application is a drawing / note application, I have a rollback / re-stack with the last ten UIImage(I don't know if this is the most efficient way) in the array. When the user makes changes to the current image, the old image is pushed onto the stack, and the first image in the array is deleted if there are already ten objects in the array. I have an instance variable intthat I use to track objects in an array and make sure the correct image is displayed. My code is as follows:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    if (oldImagesArrays.count >= 10) {
        [oldImagesArrays removeObjectAtIndex:0];
    }
    UIImage * currentImage = pageView.canvas.image;
    if (currentImage != nil) {
        [oldImagesArrays addObject:currentImage];
        undoRedoStackIndex = oldImagesArrays.count -1;
    }
    [...]
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

    UIImage * currentImage = [oldImagesArrays lastObject];
    if (currentImage != pageView.canvas.image) {
        [undoManager registerUndoWithTarget:self selector:@selector(resetImage)  
        object:currentImage];
    }
}

// Gets called when the undo button is clicked
- (void)undoDrawing
{
    [undoManager undo];
    [undoManager registerUndoWithTarget:self 
                           selector:@selector(resetImage)
                             object:pageView.canvas.image];
    undoRedoStackIndex--;
}

// Gets called when the redo button is clicked
- (void)redoDrawing
{
    [undoManager redo];
    undoRedoStackIndex++;
}

- (void)resetImage
{
    NSLog(@"Hello"); // This NSLog message only appears when I click undo.
    pageView.canvas.image = [oldImagesArrays objectAtIndex:undoRedoStackIndex];
}

, resetImage ( undoRedoStackIndex), , , .

&& || .

+5
1

, .

:

- (void)setImage:(UIImage*)image
{
    if (_image != image)
    {
        [[_undoManager prepareWithInvocationTarget:self] setImage:_image]; // Here we let know the undo managed what image was used before
        [_image release];
        _image = [image retain];

        // post notifications to update UI
    }
}

. , [_undoManager undo], [_undoManager redo]. , . Undo, [NSUndoManager canUndo] ..

. - , removeAllActions.

+6

All Articles