How to implement undo/redo with programatic change of the textValue of a NSTextView?

后端 未结 3 2153
日久生厌
日久生厌 2021-02-20 15:34

I created a simple demo app with a NSTextView and a button, the provided a NSTextViewDelegate to the textView and added an action:

- (IBAction)actionButtonClicke         


        
3条回答
  •  孤街浪徒
    2021-02-20 15:59

    -setString: is an inherited method from NSText. To handle this using NSTextView methods only so that undo is handled, just do this:

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

    Making the text change this way avoids mucking about with the undo manager at all.

提交回复
热议问题