CALayer won't display

Posts: 1
Joined: 2010.03
Post: #1
I'm trying to learn how to use CALayers for a project and am having trouble getting sublayers to display. I created a vanilla View-based iPhone app in XCode for these tests. The only real code is in the ViewController which sets up the layers and their delegates. There is a delegate, DelegateMainView, for the viewController's view layer and a second different one, DelegateStripeLayer, for an additional layer. The ViewController code is all in awakeFromNib,

- (void)awakeFromNib {

     DelegateMainView *oknDelegate = [[DelegateMainView alloc] init];
     self.view.layer.delegate = oknDelegate;

     CALayer *newLayer = [CALayer layer];
     DelegateStripeLayer *sldDelegate = [[DelegateStripeLayer alloc] init];
     newLayer.delegate = sldDelegate;

     [self.view.layer addSublayer:newLayer];

     [newLayer setNeedsDisplay];
     [self.view.layer setNeedsDisplay];

The two different delegates are simply wrappers for the CALayer delegate method, drawLayer:inContext:, i.e.,

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)context {

     CGRect bounds = CGContextGetClipBoundingBox(context);
          ... do some stuff here ...

each a bit different. The layer, view.layer, is drawn properly but newLayer is never drawn. If I put breakpoints in the two delegates, the program stops in DelegateMainView but never reaches DelegateStripeLayer. What am I missing here? Thanks.
Quote this message in a reply
Posts: 133
Joined: 2008.05
Post: #2
1. Never write any code that involves a view controller's view in awakeFromNib (or init). Do any additions to the view hierarchy in viewDidLoad.

2. Never set the delegate of a layer that is owned by a view. A layer that is owned by a view already has a delegate: that view.

Fixing those two problems will most likely fix your issue. Also, in practice, you typically don't create a special object to act as the layer delegate. You might as well just subclass CALayer at that point. Let your controller be the delegate or set the contents of the layer with an image.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  UIView tree vs CALayer tree charshep 1 6,399 May 7, 2009 06:09 PM
Last Post: longjumper
  CALayer drawLayer:inContext: dcavanagh 2 18,035 May 1, 2009 10:07 AM
Last Post: dcavanagh