简体   繁体   English

有关NSUndoManager的帮助

[英]Help with NSUndoManager

Ok, So i want to add the ability to undo certain actions in my app. 好的,所以我想在我的应用程序中添加撤消某些操作的功能。 I was going to create a way to do it, with my own protocals or something, but then I found out about NSUndoManager . 我打算用自己的协议或类似的东西来创建一种方法,但是后来我发现了NSUndoManager I would like to use the built in foundation way, but I can't seem to figure it out. 我想使用内置的基础方式,但似乎无法弄清楚。 I need to undo multiple dice rolling, so if I could store the previous rolls, as objects in an NSArray , would probably be the best. 我需要撤消多次掷骰子,因此,如果我可以存储先前的掷骰,因为NSArray对象可能是最好的。 I could use an NSMutableString , but the array would be preferred. 我可以使用NSMutableString ,但最好使用数组。

Then I know you can shake to undo, but I would rather have a button. 然后,我知道您可以摇晃以撤消操作,但是我希望有一个按钮。 These has been giving me the most trouble. 这些一直给我带来最大的麻烦。 I've included my attempts below. 我在下面列出了我的尝试。 None of those have worked. 这些都不起作用。 Any help would be appreciated. 任何帮助,将不胜感激。

In viewDidLoad: 在viewDidLoad中:

undoManager = [[NSUndoManager alloc] init];

Then in the method that rolls the dice, I tried: 然后在掷骰子的方法中,我尝试了:

   [[undoManager prepareWithInvocationTarget:self] undoButton];
    [[undoManager prepareWithInvocationTarget:self] 
    [[undoManager prepareWithInvocationTarget:self] setString:[NSString stringWithFormat:@"%i", dice1num]];

setStrings:[NSArray arrayWithObjects:[NSString stringWithFormat:@"%i", dice1num]
    [NSString stringWithFormat:@"%i", dice1num],
    [NSString stringWithFormat:@"%i", dice1num],
    [NSString stringWithFormat:@"%i", dice1num],
    [NSString stringWithFormat:@"%i", dice1num], nil]];
[[undoManager prepareWithInvocationTarget:@selector()];
[undoManager setActionName:@"A roll"];

And then here is the IBAction that links to the undo button: 然后是链接到撤消按钮的IBAction:

-(IBAction)undoButton{
           [undoManager undo];
}

Thanks in advance 提前致谢

I think you have gotten the function of the NSUndoManager wrong. 我认为您弄错了NSUndoManager的功能。 An undo manager is like a stack of invocations which are needed to reverse the thing you just did. 撤消管理器就像一堆调用,它们需要撤消您刚做的事情。 So in principle your idea is right to use a collection object in your case an NSArray to store multiple undo steps. 因此,原则上,您的想法是在您的情况下使用一个NSArray来存储多个撤消步骤的集合对象是正确的。 Unfortunately it works a litte bit different. 不幸的是,它的工作方式有点不同。

How NSUndoManager works NSUndoManager工作方式

An undo manager works by maintaining a stack of undo steps in memory. 撤消管理器通过在内存中维护一堆撤消步骤来工作。 For each step in your case dice roll you want to undo you register an invocation object using the - (void)registerUndoWithTarget:self selector:@selector(setMyObjectTitle:) object:currentTitle method. 对于案例骰子滚动中的每个步骤,要撤消操作,请使用- (void)registerUndoWithTarget:self selector:@selector(setMyObjectTitle:) object:currentTitle方法来注册调用对象。 You do this for every dice roll. 您在每次掷骰子时都要这样做。 Do only register one operation at a time. 一次只能注册一个操作。

For undoing you call the undo method as you did and what happens then is that the undo manager pops an operation of the undo stack and executes it. 要撤消操作,请像以前一样调用undo方法,然后发生的事情是撤消管理器弹出撤消栈的操作并执行它。

Your main mistake was to try to reinvent how the undo manager works. 您的主要错误是尝试重新发明撤消管理器的工作方式。 Do not register an array, just use one dice roll at a time. 不要注册数组,一次只能使用一个骰子卷。

Apple's undo architecture manual Apple的undo架构手册

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM