简体   繁体   English

NSUndoManager以及如何使重做工作

[英]NSUndoManager and How Do I get the Redo Working

I have an app where I want to save the current state of a project before every change so that undo and redo will be able to step back and forth within those states. 我有一个应用程序,我希望在每次更改之前保存项目的当前状态,以便撤消和重做将能够在这些状态内来回切换。 When I make a change the code to save the position is: 当我进行更改时,保存位置的代码是:

if(!undoManager){
    undoManager = [[NSUndoManager alloc] init];
}
[[undoManager prepareWithInvocationTarget:self] replaceSelf:currentState];

And my undo/redo functions: 我的撤消/重做功能:

-(void)undo{
    if(undoManager){
        [undoManager disableUndoRegistration];
        [undoManager undo];
        [undoManager enableUndoRegistration];
    }
}
-(void)redo{
    if(undoManager){
        [undoManager disableUndoRegistration];
        [undoManager redo];
        [undoManager enableUndoRegistration];
    }
}

The replace self function just takes the project state property and distributes the information so that the state is restored. 替换self函数只接受项目状态属性并分发信息,以便恢复状态。

Undo works perfect, i can perform 5 steps and then hit undo 5 times to rewind to the start, but redo does nothing at all! 撤消工作完美,我可以执行5个步骤,然后按5次撤消以倒回到开始,但重做什么都不做! Shouldn't invoking redo cause it to hit my selector with the last object that was undone? 不应该调用重做导致它使用撤消的最后一个对象击中我的选择器? What am I misunderstanding here? 我在这里误解了什么?

I got this working, in the replaceSelf function that was being called by the undo/redo, I added this code: 我得到了这个工作,在undo / redo调用的replaceSelf函数中,我添加了这段代码:

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

Where ps is the current project state before the undo/redo is done. 其中ps是撤消/重做完成之前的当前项目状态。 This got it working for me! 这让它对我有用!

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

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