Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

drawLayer:inContext - Unrecognized selector sent to instance

I'm trying to create a UIViewController that draws a layer and it works fine if this UIViewController is the main one. However, if I try to initialize it inside another controller and then add it's view as a sub view of the main controller's view it results in the following error:

-[__NSCFType drawLayer:inContext:]: unrecognized selector sent to instance 0x155140

Here is the relevant code for my custom UIViewController (PDFPageViewController):

- (void)loadDocument:(PDFDocument *)document
{
    self._document = document;

    CGRect pageRect = CGRectIntegral(CGPDFPageGetBoxRect(self._document.page, kCGPDFCropBox));

    pageRect.origin.x = (self.view.frame.size.width / 2) - (pageRect.size.width / 2) - 35;

    CATiledLayer *tiledLayer = [CATiledLayer layer];
    tiledLayer.delegate = self;
    tiledLayer.tileSize = CGSizeMake(1024.0, 1024.0);
    tiledLayer.levelsOfDetail = 1000;
    tiledLayer.levelsOfDetailBias = 1000;
    tiledLayer.frame = pageRect;

    contentView = [[UIView alloc] initWithFrame:pageRect];
    [contentView.layer addSublayer:tiledLayer];

    CGRect viewFrame = self.view.frame;
    viewFrame.origin = CGPointZero;

    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:viewFrame];
    scrollView.delegate = self;
    scrollView.contentSize = pageRect.size;
    scrollView.maximumZoomScale = 1000;
    [scrollView addSubview:contentView];

    [self.view addSubview:scrollView];   

    NSLog(@"%@", self); // Just checking if there's nothing overwriting the layer's delegate
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return contentView;
}

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
{
    if(self._document) {
        CGContextSetRGBFillColor(ctx, 1.0, 1.0, 1.0, 1.0);
        CGContextFillRect(ctx, CGContextGetClipBoundingBox(ctx));
        CGContextTranslateCTM(ctx, 0.0, layer.bounds.size.height);
        CGContextScaleCTM(ctx, 1.0, -1.0);
        CGContextConcatCTM(ctx, CGPDFPageGetDrawingTransform(self._document.page, kCGPDFCropBox, layer.bounds, 0, true));
        CGContextDrawPDFPage(ctx, self._document.page);
    }
}

The drawLayer method is there and the CALayer's delegate is self.

And this is how I call it on my main controller:

pageViewController = [[[PDFPageViewController alloc] initWithNibName:NULL bundle:NULL] autorelease];
[pageViewController loadDocument:self.document];

[self.view addSubview:[pageViewController view]];

Am I not doing this the proper way? I can't understand why this works fine if I draw the layer on my main controller and it results on an error if the draw is made on PDFViewController. The method is in there, the delegate is self. So why is the selector failing?

like image 711
Raphael Avatar asked Oct 10 '22 14:10

Raphael


1 Answers

[Re-posted from the comments because it turned out to be the answer!]

This sounds like a memory management problem. Have you tried NSZombieEnabled? Is there any chance you've over-released something? Are you using ARC?

like image 97
jtbandes Avatar answered Oct 19 '22 07:10

jtbandes