And*_*rew 0 iphone undo foundation nsundomanager
好的,所以我想在我的应用程序中添加撤消某些操作的功能。我本来打算用自己的协议或类似的东西来创建一种方法,但是后来我发现了NSUndoManager。我想使用内置的基础方式,但似乎无法弄清楚。我需要撤消多次掷骰子的操作,因此,如果我可以存储以前的掷骰,因为的对象NSArray可能是最好的。我可以使用NSMutableString,但最好使用数组。
然后,我知道您可以摇晃以撤消操作,但是我希望有一个按钮。这些一直给我带来最大的麻烦。我在下面列出了我的尝试。这些都不起作用。任何帮助,将不胜感激。
在viewDidLoad中:
undoManager = [[NSUndoManager alloc] init];
Run Code Online (Sandbox Code Playgroud)
然后在掷骰子的方法中,我尝试了:
[[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"];
Run Code Online (Sandbox Code Playgroud)
然后是链接到撤消按钮的IBAction:
-(IBAction)undoButton{
[undoManager undo];
}
Run Code Online (Sandbox Code Playgroud)
提前致谢
我认为您NSUndoManager错了功能。撤消管理器就像一堆调用,它们需要撤消您刚做的事情。因此,原则上,您的想法是在您的情况下使用集合对象NSArray来存储多个撤消步骤是正确的。不幸的是,它的工作方式有点不同。
NSUndoManager工作撤消管理器通过在内存中维护一堆撤消步骤来工作。对于案例骰子掷骰中的每个步骤,要撤消操作,请使用- (void)registerUndoWithTarget:self selector:@selector(setMyObjectTitle:) object:currentTitle方法注册一个调用对象。您在每次掷骰子时都要这样做。一次只能注册一个操作。
要撤消操作,请像以前一样调用undo方法,然后发生的事情是撤消管理器弹出撤消栈的操作并执行它。
您的主要错误是尝试重新发明撤消管理器的工作方式。不要注册数组,一次只能使用一个骰子卷。