2012-11-22 29 views
0

我正在以编程方式在函数中创建标签并将它们放入NSMutableArray,然后从另一个函数中删除它们。 问题是标签实际上从屏幕上消失了,但它们仍在使用内存,一段时间后程序开始工作得非常慢。RemoveFromSuperview不清理内存

这里是我的代码:

这是创建标签的功能。

- (void)CrearEstrellas{ 
    for(int i=0; i< 10; i++) 
    { 
     float x = arc4random() %300; 
     float y = arc4random() %100; 
     UILabel *estrella = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, 4, 4)]; 
     estrella.tag = i; 
     estrella.center = CGPointMake(x,y-100); 
     estrella.text = @"."; 
     estrella.textColor = [UIColor whiteColor]; 
     [self.view.superview addSubview: estrella]; 
     [arrayEstrellas insertObject:(estrella) atIndex: i]; 
    } 

} 

这是从上海华删除功能:

- (void)Lineatiempo{ 
    for(int i=0; i<[arrayEstrellas count]; i++) 
    { 
     UILabel *estrella = [arrayEstrellas objectAtIndex:(i)]; 
     float x = estrella.center.x; 
     float y = estrella.center.y; 
     estrella.center = CGPointMake(x,y+10); 
     if(estrella.center.y>200){ 
      [estrella removeFromSuperview]; 
      estrella = nil; 
     } 
    } 
} 

我想知道我在做什么错了!谢谢。

回答

1

将视图添加到数组。 NSArray(和NSMutableArray)保留您添加到它们的对象。直到您将它们从阵列中删除后才会释放它们。

所以除了调用removeFromSuperview之外,还必须从数组中删除视图。

+0

就是这样,我[arrayEstrellas removeObjectAtIndex:i];现在工作正常。非常感谢你! – Legendar

+0

不要忘记调整你的循环。如果您在迭代时删除项目,则最终会忽略某些视图。 – DrummerB