在派生 类 中正确实现 NSView drawRect

Correct implementation of NSView drawRect in derived classes

是否需要从覆盖的 drawRect 中调用 [super drawRect:dirtyRect]? 我见过 [super drawRect:dirtyRect]

的例子

根本没有被调用

-(void)drawRect:(NSRect) dirtyRect
{
 // derived class code here
}

在派生 class 代码之前被调用

-(void)drawRect:(NSRect)dirtyRect
{
 [super drawRect:dirtyRect];
 // derived class code here
}

在导出 class 代码后被调用

-(void)drawRect:(NSRect)dirtyRect
{
 // derived class code here
 [super drawRect:dirtyRect];
}

都是正确的(尤其是不调用 super drawRect)按照标准或规范或者它们只是碰巧在工作,可能会中断一些时间。我的意思是这是一个简单的继承案例,其中派生 class 必须覆盖考虑基础 class 行为吗?

有参考的回答会很有帮助。

Per NSView Docs

The default implementation does nothing. Subclasses should override this method if they do custom drawing.

...

If your custom view is a direct NSView subclass, you do not need to call super. For all other views, call super at some point in your implementation so that the parent class can perform any additional drawing.