如何使用NSUndoManager实施撤消? [英] How to implement undo using NSUndoManager?

查看:186
本文介绍了如何使用NSUndoManager实施撤消?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在程序中添加NSUndoManager,但是我不确定如何在管理器中注册方法?使用:

I am trying to add an NSUndoManager to my program but I am not sure how to register the methods with the manager? using :

[myUndoManager registerUndoWithTarget:selector:object:];

如果我有以下方法怎么办?

what if I have the following method:

-(IBAction)tapButton:(id)sender {
   myFoo++;    
   yourFoo++;    //incrementing integers
   fooFoo++;
}

如何在撤消管理器中注册此方法?选择器(发送者)的对象不是我想要注册的对象.我需要通过一次撤消调用来减少myFoo,yourFoo和fooFoo.

How can I register this method with the undo manager? The object of the selector (sender) is not what I want to register. I need to decrement myFoo, yourFoo, and fooFoo with a single undo call.

推荐答案

编写另一个方法-(void) decrementIntegers并注册如下:

Write another method -(void) decrementIntegers and register that like this:

[undoManager registerUndoWithTarget: self selector: @selector( decrementIntegers ) object: nil];

在这种方法中,您需要再次注册原始方法以提供重做:

In this method you need to register your original method again to provide redo:

[undoManager registerUndoWithTarget: self selector: @selector( tapButton: ) object: self];

但是执行此操作的更好方法是对整数使用访问器,并在其中撤消注册.像这样:

But a better way to do this would be to use accessors for your integers and do undo registering in there. Something like this:

- (void) setMyFoo: (int) newMyFoo;
{
   if (myFoo != newMyFoo) {
      [[undoManager prepareWithInvocationTarget: self] setMyFoo: myFoo];
      myFoo = newMyFoo;
   }
}

这篇关于如何使用NSUndoManager实施撤消?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆