在Cocoa中使用NSUndoManager可以很方便的完成撤销操作。NSUndoManager会记录下修改、撤销操作的消息。这个机制使用两个NSInvocation对象栈。
NSInvocation会把消息(选择器和接受者及参数)包装成一个对象,这个对象就是NSInvocation的实例。当一个对象收到它不理解的消息时,消息发送机制会在报出错误前检查该对象是否实现了forwardInvocation这个方法。如果实现了,就会将消息打包成NSInvocation对象,然后调用forwardInvocation方法。
当进行操作时,控制器会添加一个该操作的逆操作的invocation到Undo栈中。当进行Undo操作时,Undo操作的逆操作会倍添加到Redo栈中,就这样利用Undo和Redo两个堆栈巧妙的实现撤销操作。
这里需要注意的是,堆栈中存放的都是NSInvocation实例。
假设在我们的程序中有walkLeft以及这个方法的逆方法walkRight,我们可以这样来实现撤销功能。
- (void) walkLeft
{
position = position + 10;
[[undoManager prepareWithInvocationTarget:self] walkRight];
[self showTheChangesToThePostion];
}
prepareWithInvocationTarget:方法记录了target并返回UndoManager,然后UndoManager重载了forwardInvocation方法,也就将walkRight方法的Invocation添加到undo栈中了。
- (void) walkRight
{
position = position - 10;
[[undoManager prepareWithInvocationTarget:self] walkLeft];
[self showTheChangesToThePostion];
}
[undoManager setActionName:@"Insert"];
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
undoManager = [[NSUndoManager alloc]init];
[undoManager setLevelsOfUndo:999];
}
-(void)buttonClick:(id)sender
{
UIButton * btn = (UIButton*)sender;
switch (btn.tag) {
case 1:
[self.undoManager undo];
break;
case 2:
[self.undoManager redo];
break;
case 3:
[self add];
break;
case 4:
[self sub];
break;
default:
break;
}
}
-(void)add{
int nu = [num.text integerValue] + 10;
[num setText:[NSString stringWithFormat:@"%d",nu]];
[[undoManager prepareWithInvocationTarget:self] sub];
}
-(void)sub
{
int nu = [num.text integerValue] - 10;
[num setText:[NSString stringWithFormat:@"%d",nu]];
[[undoManager prepareWithInvocationTarget:self] add];
}