内存中的对象在仍然绘制旧对象时被破坏并重新绘制

时间:2015-10-26 11:45:21

标签: ios objective-c memory

所以我认为我的旧对象正在从内存中被摧毁所以我每10次打开应用程序就会再次运行drawRect,但不幸的是旧对象仍然被新对象所吸引!它不起作用(只有新对象可以,但我仍然可以看到旧对象)。

//Only gets called once
-(id)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super initWithCoder:aDecoder]) {
        [self initialSetup];

    }
    return self;
}

 //gets called every 10th time or so app is opened and closed but old object is still drawn in background

-(void)drawRect:(CGRect)rect
{
    // Draw for interface builder
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGRect myFrame = CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);
    // bunch of code I took out from here
    UIRectFrame(myFrame);
    CGContextFillRect(context, myFrame);
    UIGraphicsEndImageContext();
}

当drawRect第二次运行时,如何摆脱对象的绘制?

1 个答案:

答案 0 :(得分:1)

drawRect:不打算自动从内存中删除现有的类似对象。

在添加新对象并添加视图层次结构之前,您必须保留对从此UIView子类创建的对象的引用,并在完成2行之后调用:

[self.myCustomView removeFromSuperview];
self.myCustomView = nil;