我正在创建一个加载屏幕UIView,它被添加到子视图中,而某些XML是从某个URL解析的。返回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
}
答案 0 :(得分:2)
当您释放loadingScreen时,将其重置为零值。
[loadingScreen release];
loadingScreen = nil;
[nil release]不会发生任何事情。