如何设置特定的重做操作与使用prepareWithInvocationTarget
使用我的方法,重做不起作用(撤消工作)
- (void)removeSmth:(Smth *)smth index:(NSInteger)indexOfSmth {
[self.document.undoManager beginUndoGrouping];
...
[self removeSmth:smth];
[[self.document.undoManager prepareWithInvocationTarget:self] undoInsertSmth:smth index:indexOfSmth];
...
[self.document.undoManager endUndoGrouping];
}
- (void)undoInsertSmth:(Smth *)smth index:(NSUInteger)index {
[self insertSmth:smth index:index];
}
答案 0 :(得分:1)
在undo方法中,如果从撤消调用,则应注册撤消
- (void)removeSmth:(Smth *)smth index:(NSInteger)indexOfSmth {
[self.document.undoManager beginUndoGrouping];
...
[self removeSmth:smth];
[[self.document.undoManager prepareWithInvocationTarget:self] undoInsertSmth:smth index:indexOfSmth];
...
[self.document.undoManager endUndoGrouping];
}
- (void)undoInsertSmth:(Smth *)smth index:(NSUInteger)index {
if ([self.document.undoManager isUndoing]) {
[[self.document.undoManager prepareWithInvocationTarget:self] removeSmth:smth index:index];
}
[self insertSmth:smth index:index];
}