[英]How can I deal with a Subview memory leak?
泄漏儀器在某些代碼上發出警報,但我不知道如何解決泄漏而不崩潰應用程序。 這是一些代碼,概述了我的方法,它是在一段時間之前編寫的,顯然需要重新考慮:
labels = [[NSMutableArray alloc] init];
for(int i = 0; i < 10; i++) {
// calculate x and y...
label = [[UILabel alloc] initWithFrame:CGRectMake(x, y, 70, 15)];
// customize label...
[labels addObject:label];
[label release];
[self addSubview:[labels objectAtIndex:i]];
}
為什么要麻煩標簽NSMutableArray? 后來,在其他方法中,我需要更改標簽的Alpha,這樣說起來很方便
[[labels objectAtIndex:num] setAlpha:0.5];
我相信會發生泄漏是因為標簽只有在退出時才取消分配超級視圖,因此在正常的應用程序生命周期中不會取消分配標簽。
救命!
謝謝。
這可能是因為您沒有取消分配標簽數組。 在for循環后釋放標簽數組,然后使用.tag在UIView上設置標簽,然后稍后使用viewWithTag到setAlpha通過.tag查找標簽,該怎么辦?
labels = [[NSMutableArray alloc] init];
for(int i = 0; i < 10; i++) {
// calculate x and y...
label = [[UILabel alloc] initWithFrame:CGRectMake(x, y, 70, 15)];
label.tag = 100;
// customize label...
[labels addObject:label];
[label release];
[self addSubview:[labels objectAtIndex:i]];
}
[labels removeAllObjects];
[labels release];
然后再
(UILabel*) [[self.view viewWithTag:100] setAlpha:0.5];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.