1
votes

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.

2
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
I changed the name of the class in the identity inspectorHenry Rillovian
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
Hi Rein, I added a GitHub project, please see above. Just run, enter characters into the text view, and try to undo.Henry Rillovian
No overrides and no new code?Willeke

2 Answers

0
votes

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
0
votes

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