0

我正在编写一个相当简单的应用程序,它生成一个线程,该线程最终调用以下方法将 UILabel 放在某个位置。我曾期望 ARC 在方法关闭时清理标签。我错了。:)

有没有办法强制清理这些东西,或者有什么明显的我遗漏的东西?谢谢!

-(void) drawNumberLabel:(NSString *)labelText xloc:(float)xLocation yLoc:(float)yLocation {

    UILabel *tempLabel;
    tempLabel = [[UILabel alloc] initWithFrame:CGRectMake(xLocation, yLocation, 27.0, 59.0)];
    tempLabel.font = [UIFont fontWithName:@"Helvetica" size:fontSize];
    tempLabel.text = labelText;
    tempLabel.backgroundColor = backgroundColor;
    tempLabel.textColor = textColor;
    [self addSubview:tempLabel];

}
4

2 回答 2

1

“清理标签”是什么意思?您是否期望在此方法结束时释放 tempLabel?它不会,因为当您调用时[self addSubview:tempLabel],您的视图会保留标签。当 superview 被释放时,您添加的标签也将被释放。

于 2011-09-22T18:39:48.553 回答
0

When you are using ARC (Automatic Reference Counting), you should never make any memory management calls because the compiler will insert these statements for you at compile-time.

The compiler should be injecting [tempLabel release]; to the end of your method at compile-time.

However, because you have added the label as a subview to a view, the containing view will retain the label, and the label will not be released until you remove the label from that view.

于 2011-09-07T04:26:45.390 回答