[Не сохранен тип]: сообщение отправлено в освобожденный экземпляр, цель c

Я настраиваю свой вид с помощью градиента и добавляю к нему ulabel, делая следующее:

@implementation ECertificateViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
    CustomViewBackGround  *bgView       =   [[CustomViewBackGround alloc] initWithFrame:CGRectMake(0, 0, 301, 26)];
    [self.mainView addSubview:bgView];
}


#import "CustomViewBackGround.h"

@implementation CustomViewBackGround
- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
        CGFloat height                  =   20.0;
        CGFloat x                       =   5;
        CGFloat y                       =   3;
        titleLabel                      =   [[UILabel alloc] initWithFrame:CGRectMake(x, y, self.bounds.size.width - 2 * x, height)] ;
        titleLabel.text                 =   @"This is my label";
        titleLabel.textAlignment        =   NSTextAlignmentLeft;
        titleLabel.opaque               =   NO;
        titleLabel.backgroundColor      =   [UIColor clearColor];
        titleLabel.font                 =   [UIFont boldSystemFontOfSize:14];
        titleLabel.textColor            =   [UIColor lightGrayColor];
        [self addSubview:titleLabel];
    }
    return self;
}

- (void)drawRect:(CGRect)rect {
    CGContextRef context            =   UIGraphicsGetCurrentContext();

    CGColorRef whiteColor           =   [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0].CGColor;
    CGColorRef lightGrayColor       =   [UIColor colorWithRed:230.0/255.0
                                                        green:230.0/255.0
                                                         blue:230.0/255.0
                                                        alpha:1.0].CGColor;
    CGColorRef separatorColor       =   [UIColor colorWithRed:208.0/255.0 green:208.0/255.0 blue:208.0/255.0 alpha:1.0].CGColor;

    CGRect paperRect                =   self.bounds;

    // Fill with gradient
    drawLinearGradient(context, paperRect, whiteColor, lightGrayColor);

    // Add white 1 px stroke
    CGRect strokeRect           =   paperRect;
    strokeRect.size.height     -=   1;
    strokeRect                  =   rectFor1PxStroke(strokeRect);

    CGContextSetStrokeColorWithColor(context, whiteColor);
    CGContextSetLineWidth(context, 1.0);
    CGContextStrokeRect(context, strokeRect);

    // Add separator
    CGPoint startPoint          =   CGPointMake(paperRect.origin.x, paperRect.origin.y + paperRect.size.height - 1);
    CGPoint endPoint            =   CGPointMake(paperRect.origin.x + paperRect.size.width - 1, paperRect.origin.y + paperRect.size.height - 1);
    draw1PxStroke(context, startPoint, endPoint, separatorColor);            
}

Когда я работаю в симуляторе, все работает нормально, как я и ожидал. Однако, когда я устанавливаю на устройство, приложение вылетает, и я получаю

[Not A Type retain]: message sent to deallocated instance,objctive c

У вас есть идеи, почему я получаю эту проблему?


person tranvutuan    schedule 03.04.2013    source источник


Ответы (1)


Попробуй это:

UIColor *whiteColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0];
...
drawLinearGradient(context, paperRect.CGColor, whiteColor.CGColor, lightGrayColor.CGColor);

Или это:

CFColorRef whiteColor = CFBridgingRetain([UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0].CGColor);
...
drawLinearGradient(context, paperRect, whiteColor, lightGrayColor);
...
CFRelease(whiteColor);
...

Возможно, ARC выпускает ваши UIColor, как только вы их перестаете использовать, и, освобождая их, их свойства CGColor также освобождаются, поскольку их никто не сохранил.

person Guillaume    schedule 03.04.2013
comment
Я попробовал первый, и он работал для меня. Спасибо за вашу помощь. - person tranvutuan; 03.04.2013