何时释放从超级视图中删除的 UIView
我正在创建一个加载屏幕 UIView,它被添加到子视图中,同时从某个 URL 解析一些 XML。返回 XML 后,加载屏幕将从其超级视图中删除。
我的问题是我应该如何释放这个对象?
在下面的代码中,您会看到我将removeFromSuperview
发送到loadingScreen,但我仍然拥有这个对象的所有权对象除非我释放它。但是,如果我释放它,viewdidUnload
和 dealloc
中将没有任何内容可以释放。
- (void)loadView {
...
loadingScreen = [[LoadingScreen alloc] initWithFrame: self.view.frame];
[self.view addSubview:loadingScreen]; //retain count = 2
}
-(void)doneParsing {
...
[loadingScreen removeFromSuperview]; //retain count = 1
[loadingScreen release]; //should i release the loading screen here?
}
- (void)viewDidUnload {
[loadingScreen release]; //if viewDidUnload is called AFTER doneParsing, this
//will cause an exception, but the app might crash before
//doneParsing is called, so i need something here
}
- (void)dealloc {
[loadingScreen release]; //if i've already released the object, i can't release here
}
I am creating a loading screen UIView which is added to a subview while some XML is parsed from some URL. Once the XML is returned, the loading screen is removed from its superview.
The question I have is how should I release this object?
In the code below, you'll see that I send removeFromSuperview
to the loadingScreen, but I still have ownership of this object unless I release it. But, if I release it, there'll be nothing there to release in viewdidUnload
and dealloc
.
- (void)loadView {
...
loadingScreen = [[LoadingScreen alloc] initWithFrame: self.view.frame];
[self.view addSubview:loadingScreen]; //retain count = 2
}
-(void)doneParsing {
...
[loadingScreen removeFromSuperview]; //retain count = 1
[loadingScreen release]; //should i release the loading screen here?
}
- (void)viewDidUnload {
[loadingScreen release]; //if viewDidUnload is called AFTER doneParsing, this
//will cause an exception, but the app might crash before
//doneParsing is called, so i need something here
}
- (void)dealloc {
[loadingScreen release]; //if i've already released the object, i can't release here
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当你释放loadingScreen时,将其重置为nil值。
[nil release] 不会发生任何事情。
When you release loadingScreen, reset it to a nil value.
[nil release] won't happen anything.