我在处理内存管理时遇到了一些问题。我使用循环将图像和按钮添加到主视图。图像正确释放,但不是按钮。使用UIButton进行内存管理
我的问题归结为,给出以下代码,为什么当我按下按钮来清除屏幕,内存不释放? UIButton是从UIButtonType自定义的autoreleased,并且setImage autoreleases交通图。
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *homeButton = [UIButton buttonWithType:UIButtonTypeCustom];
[homeButton setFrame:[UIScreen mainScreen].bounds];
[homeButton setBackgroundColor:[UIColor blueColor]];
[homeButton setImage:[UIImage imageNamed:@"TransportationMap.png"] forState:UIControlStateNormal];
homeButton.tag = 9399;
[homeButton addTarget:self action:@selector(clearScreen) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:homeButton];
}
-(void)clearScreen
{
for (UIView *view in [self.view subviews]) {
[view removeFromSuperview];
}
}
你是什么意思的“图像正确释放,但不是按钮”? – highlycaffeinated
这是参考我的实际程序,我添加图像和文本,以及按钮。我没有正确释放图像的问题,只有按钮。这只是代码示例,我用自定义图像实现了一个按钮,并且在将按钮从屏幕上移除后内存泄漏 – Dylan