Adding a CALayer Sublayer Inside a UIView init

I am trying to add CALayer as a sublayer in a subclass of UIView, but when I add a sublayer inside the init method, I get EXC_BAD_ACCESSwhen I add a view to another view or window.

Init Method:

- (id)initWithTitle:(NSString *)title message:(NSString *)message
{
    if ((self = [super init]))
    {
        self.title = title;
        self.message = message;

        self.alertLayer = [[CALayer alloc] init];

        self.layer.cornerRadius = kCORNER_RADIUS;
        self.layer.shadowRadius = 3.0;
        self.layer.shadowColor = [UIColor blackColor].CGColor;
        self.layer.shadowOffset = CGSizeMake(15, 20);
        self.layer.shadowOpacity = 1.0;

        self.alertLayer.delegate = self;
        self.alertLayer.masksToBounds = YES;
        self.alertLayer.cornerRadius = kCORNER_RADIUS;

        [self.layer addSublayer:self.alertLayer]; // This line of code seems to cause EXC_BAD_ACCESS
    }

    return self;
}

EXC_BAD_ACCESScalled after a call [self.view addSubview:alertView]inside the view controller or UIWindow.

+5
source share
1 answer

(self.layer self.alertLayer), self, -[UIView(Hierarchy) _makeSubtreePerformSelector:withObject:withObject:copySublayers:] (self) , self.alertLayer.delegate = self;, . alarmLayer, .

+9

All Articles