对CST进行子类化NSTextView使得无法撤消

时间:2016-10-25 16:36:45

标签: objective-c cocoa nstextview

我有一个非常简单的Cocoa程序。一个窗口,里面有一个NSTextView。在此配置中,NSTextView可以完全按照您的预期运行。我可以输入,撤消,重做,一切。

但是我继承了NSTextView,我们称之为TextView。这个新类不会覆盖任何方法,它基本上是一个空白的子类。我用我的新子类替换NSTextView。我可以在TextView中键入文本,但是undo不起作用。它只是在我身上发出哔哔声。撤消菜单项显示为灰色。我不希望发生这种情况,因为TextView不会向对象继承结构添加任何新代码。

我必须对TextView做什么才能重新启用撤消?

GitHub project

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

编辑:经过进一步的观察,我的子类确实有一个编辑引起了这个问题,就像Mohamad Farhand所说的那样。

2 个答案:

答案 0 :(得分:0)

您可能需要启用子类TextView类的allowsUndo属性。

@implementation TextView

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

@end

答案 1 :(得分:0)

覆盖方法:shouldChangeTextInRange导致此问题

我建议你查看这个链接: Cocoa: looking for a general strategy for programmatic manipulation of NSTextView storage without messing up undo