如何通过编程NSTextView的textValue来实现undo/redo?

use*_*667 10 cocoa undo-redo nstextview

我用NSTextView和一个按钮创建了一个简单的演示应用程序,为textView提供了一个NSTextViewDelegate并添加了一个动作:

- (IBAction)actionButtonClicked:(id)sender {
    NSString *oldText = [[[self.textView textStorage] string] copy];
    NSString *newText = @"And... ACTION!";

    [[self.textView undoManager] registerUndoWithTarget:self.textView
                                               selector:@selector(setString:)
                                                 object:oldText];
    [[self.textView undoManager] setActionName:@"ACTION"];

    [self.textView setString:newText];
}
Run Code Online (Sandbox Code Playgroud)

如果我手动更改文本,撤消/重做没有问题.但是,如果我使用action方法更改文本,则undo会按预期工作,但redo不再起作用(没有任何反应),并且撤消管理器似乎被加扰...

好的 - 为了避免NSTextView的问题我创建了一个模型类,将NSTextView绑定到它并将撤消/重做移动到模型,但这显示了与以前相同的行为 - 我做错了什么 - 这应该很容易,不应该吗?

#import "GFTextStore.h"

@implementation GFTextStore

@synthesize textVal = textVal_;

-(void)changeText{
    if (!undoMan_) {
        undoMan_ = [[[NSApplication sharedApplication] mainWindow] undoManager];   
    }

    NSAttributedString *oldText = [self.textVal copy];
    NSString *tempStr = [[oldText string] stringByAppendingFormat:@"\n%@",[[NSCalendarDate date]description]];
    NSAttributedString *newText = [[NSAttributedString alloc] initWithString:tempStr];

    [self setTextVal:newText];


    [undoMan_ registerUndoWithTarget:self
                            selector:@selector(setTextVal:)
                              object:oldText];

    [undoMan_ setActionName:@"ACTION"];
}
@end
Run Code Online (Sandbox Code Playgroud)

auc*_*uco 11

无需在此处添加NSUndoManager,只需让NSTextView完成工作即可.

你只需要确保你所呼叫的更高水平的方法只有 NSTextView的开头插入......而不是直接设置TextView的或textStorage文本/字符串:

    [self.textView insertText:newString];
Run Code Online (Sandbox Code Playgroud)

如果你绝对需要使用了setString或其他低级别的方法,那么你只需要添加所需的方法处理textDidChange代表团:-shouldChangeTextInRange:replacementString-didChangeText(通过插入...方法BTW完成):

if( [self.textView shouldChangeTextInRange:editedRange replacementString:editedString]) {
    // do some fancy stuff here…
    [self.textView.textStorage replaceCharactersInRange:editedRange
                          withAttributedString:myFancyNewString];
    // … and finish the editing with
    [self.textView didChangeText];
}
Run Code Online (Sandbox Code Playgroud)

这会自动让NSTextView的UndoManager的踢在我认为的UndoManager正在准备undoGrouping在shouldChangeTextInRange:和调用在didChangeText撤销:.


Rob*_*ger 5

-setString:是一种继承的方法NSText.要NSTextView仅使用方法处理此操作以便处理撤消,只需执行以下操作:

[self.textView setSelectedRange:NSMakeRange(0, [[self.textView textStorage] length])];
[self.textView insertText:@"And… ACTION!"];
Run Code Online (Sandbox Code Playgroud)

以这种方式改变文本可以避免与撤消管理器混淆.