macoscocoansviewdrawrect

Correct implementation of NSView drawRect in derived classes


Is it necessary to call [super drawRect:dirtyRect] from overridden drawRect? I have seen examples where [super drawRect:dirtyRect]

is not being called at all

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

is being called before derived class code

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

is being called after derived class code

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

Are all correct (especially not calling super drawRect) as per standard or specification OR they just happen to be working and may break some time. I mean is it a simple case of inheritance where derived class must override keeping base class behavior in consideration?

An answer with reference would be helpful.


Solution

  • 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.