سؤال

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

هل كانت مفيدة؟

المحلول

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

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top