-1

我有一个UIView对象,我将其添加为子视图,使用[self.view addSubview:mySubView];. 之后我打电话 [mySubView removeFromSuperview];,然后我尝试做,[mySubView release];但应用程序在这里崩溃。

RemoveFromSuperview叫吗releasemySubView

4

3 回答 3

4

removeFromSuperview的,释放视图。

你检查过文档吗?大约需要 5 秒:

讨论
如果接收者的superview不是nil,superview释放接收者。如果您打算重用视图,请务必retain在调用此方法之前使用它,并release在以后适当时再次使用它。

于 2012-10-10T12:14:23.237 回答
3

是的,超级视图保留其所有子视图,因此当您删除视图时,其超级视图将释放它。

于 2012-10-10T12:14:14.467 回答
2

请发布您的代码,否则很难回答

崩溃代码

UIView *subview = [[UIView alloc] init]; //manual allocation
[self.view addSubView:subview]; //view retained automatically
[subview release]; //released the manual allocated memory

for(UIView *subview in [scrollView subviews]) {    

    [subview removeFromSuperview]; //released the view retained automatically. Now retain count reach 0/
   [subview release]; // crash.....
}

没有崩溃的代码

UIView *subview = [[UIView alloc] init]; //manual allocation
[self.view addSubView:subview]; //view retained automatically   

[subview removeFromSuperview]; //released the view retained automatically. Now retain count reach 1

[subview release]; // No crash .retain count reach 0

你只是不释放你自己没有明确分配或保留的东西。

于 2012-10-10T12:23:55.170 回答