objective-c - NSUndoManager 和替换/跳过撤消步骤

标签 objective-c macos cocoa nsundomanager

是否有任何方法可以告诉 undoManager 在某些情况下,撤消步骤在编辑发生之前就已提交?

我有一个应用程序,可以自动将某些字符串替换为大写对应项。问题是,这必须在 textStorage 中自动完成,因此当撤消编辑时,文本仍保持大写。

我已经为相关操作编写了自定义撤消状态,这些状态是在 NSTextView shouldChangeTextInRange 中创建的:

例如:

[[self.undoManager prepareWithInvocationTarget:self] 
       replaceCharactersInRange:
             NSMakeRange(affectedCharRange.location + affectedCharRange.length, string.length + 1) withString:string
];

我希望 undoManager 忽略编辑后将收到的撤消步骤。

最佳答案

一种方法是使用 [self.undoManagerdisableUndoRegistration]; 禁用 NSUndoManager 记录撤消步骤。然后,操作完成后,您可以使用[self.undoManager enableUndoRegistration]再次启用它。

这似乎是一个有点危险的方法,因为它真的会弄乱你的撤消堆栈。

我通过在 NSTextView 撤消注册之前执行额外的撤消步骤解决了此问题:

[self.undoManager registerUndoWithTarget:self handler:^(id _Nonnull target) {
    [self replaceCharactersInRange:undoStringRange withString:undoString];
}];

不过,重做可能会造成损害,因为整个受影响的字符串可能无法使用 TextView 的内部方法注册。

关于objective-c - NSUndoManager 和替换/跳过撤消步骤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61897149/

相关文章:

java - mac osx 上 java7 模态对话框的焦点问题

xcode - macOS : Breakpoint when file is deleted

ios - UIbutton标题行间距

iphone - UITabBarItem标题位置

ios - 我如何在另一个线程的运行循环中安排时间?

macos - 如何在 Mac OS X Mountain Lion 上构建 mod_jk?

cocoa - 转换为 JPEG 时我应该如何考虑 CGImageAlphaInfo?

swift - Swift 中的 NSObject 子类 : hash vs hashValue, isEqual vs ==

iphone - 如何在 iPhone 上的 ViewController 之间共享对象?

ios - ProductName-Swift.h 不会在更改相应的 Objective-C 文件时更新