I have some code that I used in previous projects and there it works fine. Now I used that old code as a guideline for solving the same problem in my current project. The problem: The same approach that worked fine now crashes my app. Maybee one of you sees what I am doing wrong here?
In my current project I have a custom tableviewcell that contains a custom UIView called “VerlaufView”. I connected everything in Interface Builder and I checked that that works fine. So here is my custom UIView:
Interface definition:
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
@interface VerlaufView : UIView {
CALayer* mainLayer;
}
@property (strong,nonatomic) CALayer* mainLayer;
-(void) initialisieren;
@end
And this is the implementation. Please note that the implementation contains the inner class “VerlaufDelegate”, this will be the delegate that will handle the drawings on a calayer.
#import "VerlaufView.h"
static bool debug = YES;
#pragma mark VerlaufDelegate
@interface VerlaufDelegate : NSObject {
UIView* layersParent;
}
@property (nonatomic,strong) UIView* layersParent;
-(id)initWithView:(UIView*)view;
@end
@implementation VerlaufDelegate
@synthesize layersParent;
-(id)initWithView:(UIView*)view {
if( debug ) NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__);
self = [super init];
if( self )
self.layersParent = view;
if( debug ) NSLog(@">>> Leaving %s <<<", __PRETTY_FUNCTION__);
return self;
}
- (void)drawLayer:(CALayer*)layer inContext:(CGContextRef)ctx {
if( debug ) NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__);
if( debug ) NSLog(@">>> Leaving %s <<<", __PRETTY_FUNCTION__);
}
@end
@implementation VerlaufView
@synthesize mainLayer;
-(void) initialisieren
{
if( debug ) NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__);
VerlaufDelegate* layerDelegate = [[VerlaufDelegate alloc] initWithView:self];
float width = 200; //320;
float height = 100; //259;
//Hauptlayer
mainLayer = [[CALayer alloc] init];
mainLayer.delegate = layerDelegate;
mainLayer.bounds = CGRectMake( 0.f, 0.f, width, height );
/*mainLayer.backgroundColor = [[UIColor whiteColor] CGColor];
mainLayer.cornerRadius = 10.f;
CGFloat components[4] = { 0.0, 0.0, 0.0, 1.0 };
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
CGColorRef black = CGColorCreate( colorspace, components );
mainLayer.borderColor = black;
mainLayer.borderWidth = 1.f;
mainLayer.shadowRadius = 5.f;
mainLayer.shadowColor = black;
CGColorRelease( black );
CGColorSpaceRelease(colorspace);
mainLayer.shadowOffset = CGSizeMake( 0.f, 5.f ),
mainLayer.shadowOpacity = 0.75f;
*/
[self.layer insertSublayer:mainLayer above:self.layer];
[mainLayer setNeedsDisplay];
if( debug ) NSLog(@">>> Leaving %s <<<", __PRETTY_FUNCTION__);
}
@end
So my code crashes while processing the “initialisieren” method.
Okay, I solved the problem. Now it works. There had probably been a problem with the reference count. Therefore I put the interface declaration of my delegate into the header file. The following code solves the problem:
And the implementation:
Okay, that’s it!