Difficulty implementing NSUndoManager redo function

折月煮酒 提交于 2019-12-02 21:18:06

You do not need to keep track of the changes, this is what the undo manager is for.

Make an undoable method like this:

- (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
    }
}

This is it. To undo the change just call [_undoManager undo], to redo call [_undoManager redo]. When you tell the undo manager to undo it will call this method with the old image. If you use custom buttons for Undo operation you can validate it using [NSUndoManager canUndo], etc.

There is no limit for the number of undo operations. If you need to clean the undo stack at some point just call removeAllActions method.

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