Reputation: 2830
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.
Upvotes: 0
Views: 393
Reputation: 133239
Since NSView
itself never draws anything at all, there is no need to call super
if you inherit from NSView
, as it won't have any effect.
That's what the Apple documentation answers but what it doesn't answer is: In all other cases, do you call super
first, last or in-between?
And the correct answer is: It depends what your superclass is drawing an what your class is adding to it.
If you want to draw over whatever your superclass is drawing, you need to call it first, as otherwise the superclass hasn't drawn anything yet.
If you want to just fill the gaps that your superclass doesn't draw to, you need to call it last, so you can first create a background, then have your superclass draw on top of it and wherever it draws nothing, your background will shine through.
And if you want both, first prepare the drawing canvas, then make your superclass draw to it and finally draw over everything again where desired, you need to call it somewhere in the middle.
Upvotes: 0
Reputation: 151
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.
Upvotes: 1