我试图向块代码添加一些撤消/重做(从 NSOpenPanel 导入一些文件),只要代码源自块,主代码就可以工作,但 NSUndoManager 代码不会保存并且不会给出任何错误。NSUndoManager 代码在其他任何地方都可以工作,只要它不在块中
示例 NSUndoManager 代码如下:
// Undo Work
id undoObject = [NSApp targetForAction:@selector(undoManager) to:nil from:self];
NSUndoManager *undoManager = [undoObject undoManager];
[[undoManager prepareWithInvocationTarget:self] deleteImages];
[undoManager setActionName:@"import images"];
下面显示了代码在哪里起作用和不起作用:
/* NSUndoManager code works */
NSOpenPanel *openPanel = [NSOpenPanel openPanel];
[openPanel beginSheetModalForWindow:[self.windowController window]
completionHandler:^(NSInteger result) {
if (result == NSFileHandlingPanelOKButton) {
/* NSUndoManager code does not work,
it also does not work inside importImages: */
[self importImages:[openPanel URLs]];
} else {
[openPanel close];
}
}];
/* NSUndoManager code works */