0

According to documentation Default implementation does nothing.

But... I throw exception from drawRect method and i see next callstack

3   EasyWakeup                          0x0003a7b6 -[AlarmIntervalView drawRect:] + 71
4   UIKit                               0x003f6187 -[UIView(CALayerDelegate) drawLayer:inContext:] + 426
5   QuartzCore                          0x011a8b5e -[CALayer drawInContext:] + 143

So as i can understand it means default implementation of -[CALayer drawInContext:] call delegate method. Is it correct? Since i have known swizzling technique i'm not sure anything in objective-c...

Maxim Kholyavkin
  • 4,463
  • 2
  • 37
  • 82
  • Need to see some code. How is your code set up? Does the view have a custom layer? Are you setting the view as a delegate to multiple layers? – pe8ter Jan 11 '12 at 03:02
  • @pe8ter I create empty project and insert minor code to reproduce the issue. You could look it here: [link](http://pastebin.com/cEVNjgwR) – Maxim Kholyavkin Jan 11 '12 at 22:46
  • You're throwing an exception explicitly. Why? – pe8ter Jan 12 '12 at 01:31
  • @pe8ter I do that especially for this question. I found issue with debuger. – Maxim Kholyavkin Jan 13 '12 at 12:07
  • Okay. I get it now; you threw an exception for debugging purposes. I thought the exception was part of your problem. I added an answer to this question. – pe8ter Jan 14 '12 at 01:13

1 Answers1

4

You're correct in that CALayer's default drawInContext: does nothing. This is true unless the layer has a delegate and that layer's delegate implements drawLayer:inContext:. So the issue with the documentation is that it should have a little asterisk next to the statement "Default implementation does nothing."

Remember, all views are backed with some kind of CALayer. This layer is automatically setup to have its view set as its delegate. What's not apparent on the surface is that UIView does implement CALayer's delegate drawLayer:inContext:. This is what you're seeing with all those calls on the call stack.

Your instance of AlarmIntervalView automatically has a backing layer, and that backing layer has its delegate set to your AlarmIntervalView instance. Some part of the system calls the backing layer's drawInContext: which checks for a delegate (which it has), sends the delegate respondsToSelector: with drawLayer:inContext: as the argument (which UIView does respond to), and finally actually sends the message drawLayer:inContext:. UIView's implementation of drawLayer:inContext: calls your view's drawRect:.

I'm not really sure why you mention swizzling.

[My response is long, mostly for my benefit. It helps me learn too.]

pe8ter
  • 1,263
  • 12
  • 10