从UIScrollView中删除视图

时间:2010-09-22 04:25:56

标签: iphone ipad memory-management uiscrollview

使用Apple的PhotoScroller示例,为了重用为视图分配的内存,一旦保留计数达到0,我就无法释放内存。这里我的代码可以更好地理解: 这件作品是PhotoScroller的摘录 的 PhotoViewController.m

- (void)configurePage:(ImageScrollView *)page forIndex:(NSUInteger)index  
{  
    page.index = index;  
    page.frame = [self frameForPageAtIndex:index];  
    [page displayPage:index];  
}

ImageScrollView.h

@interface ImageScrollView : UIView  
{

    UIViewController *vc;
    NSUInteger     index;

}  
@property (assign) NSUInteger index;

- (void)displayPage:(int)indice;

@end

ImageScrollView.m

- (void)dealloc  
{    
 [vc release];  
    [super dealloc];  
}

- (void)displayPage:(int)indice  
{  

     //remove previous view  
     [vc.view removeFromSuperview];  
     [vc release];  
     vc = nil;  
     //NSLog(@"vc retain %i", [vc retainCount]);  

     // make a new viewController for the new page   
     Class clss = NSClassFromString([NSString stringWithFormat:@"page_0%i", indice + 1]);   
     vc = [[clss alloc] initWithNibName:[NSString stringWithFormat:@"page_0%i", indice + 1] bundle:nil];  

     [self addSubview:vc.view];  
}

类“page_0x”是UIViewControllers。到目前为止,只有XIB上的UIImageView。 page_01的一个例子:
    @implementation page_01

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Overriden to allow any orientation.
    return YES;
}


- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}


- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end

设备上的内存峰值达到148 MB。给我一个内存警告级别1,然后释放一些内存。降到95 MB。继续上下。

1 个答案:

答案 0 :(得分:0)

也许你的page_0x视图控制器正在被释放,但他们创建的东西却没有。有些事要检查:

  1. 在page_0x视图控制器的dealloc方法中设置断点。它被叫了吗?

  2. 检查page_0x视图控制器的所有IBOutlet和其他实例变量。它们是否都在班级dealloc方法中正确发布?

  3. 运行Build&分析。它出现了什么吗?

  4. 尝试运行Leaks Instrument。它可以告诉你实际泄漏了什么样的物体。

  5. (编辑)现在抓住稻草。你没有打开NSZombieEnabled,是吗?

  6. (编辑2)你说你正在扔一个.png。如果你删除.png会怎么样?

  7. 如果您在模拟器中运行,请在设备上进行尝试。 (见this question。)

相关问题