1

I have a very simple Cocoa program. One window, with an NSTextView inside of it. In this configuration, the NSTextView operates exactly how you would expect it to. I can type, undo, redo, everything.

But then I subclass NSTextView, lets call it TextView. This new class does not override any methods, it's basically a blank subclass. I replace the NSTextView with my new subclass of it. I can type text into the TextView, but undo does not work. It just beeps at me. The undo menu item is greyed out. I would not expect this to happen, since TextView doesn't add any new code to the object inheritance structure.

What must I do to my TextView to re-enable undo?

GitHub project

XIB object hierarchy, "Text View" is my "TextView" class

Edit: upon further observation, there indeed was an edit in my subclass that caused the issue, as Mohamad Farhand opined.

  • 1
    Exactly how did you "replace the NSTextView with my new subclass of it"? Did you remove the object and add a new one or did you just change the class of the text view in the Identity inspector? – Ken Thomases Oct 25 '16 at 22:33
  • I changed the name of the class in the identity inspector – Henry Rillovian Oct 26 '16 at 02:12
  • 1
    I just tried this and it works for me. When you replaced the text view, are you sure you didn't replace the scroll views? I can't see why it didn't work for you. Perhaps host a version of this non-working code on GitHub. – rein Oct 26 '16 at 04:57
  • Hi Rein, I added a GitHub project, please see above. Just run, enter characters into the text view, and try to undo. – Henry Rillovian Oct 26 '16 at 12:19
  • No overrides and no new code? – Willeke Oct 26 '16 at 16:30

2 Answers2

0

You may need to enable allowsUndo property of your subclassed TextView class.

@implementation TextView

- (nullable instancetype)initWithCoder:(nonnull NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        self.allowsUndo = YES;
    }
    return self;
}

@end
1024jp
  • 2,058
  • 1
  • 16
  • 25
0

overriding method : shouldChangeTextInRange cause this issue

i recommend you to check this link : Cocoa: looking for a general strategy for programmatic manipulation of NSTextView storage without messing up undo

Community
  • 1
  • 1
Mo Farhand
  • 1,144
  • 8
  • 21