I created a simple demo app with a NSTextView and a button, the provided a NSTextViewDelegate to the textView and added an action:
- (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];
}
Undo/redo works without problems, if I change text by hand. But if I change the text with the action method, undo works as expected, but redo does not work anymore (nothing happens) and the undo manager seems to be scrambled...
OK - to avoid problems with NSTextView I created a model class, bound the NSTextView to it and moved the undo/redo to the model, but this shows the same behavior as before - what the I'm doing wrong - this should be easy, shouldn't it?
#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