何时发布从superview中删除的UIView

时间:2010-09-11 14:01:06

标签: iphone objective-c memory-management uiview dealloc

我正在创建一个加载屏幕UIView,它被添加到子视图中,而某些XML是从某个URL解析的。返回XML后,加载屏幕将从其超级视图中删除。

我的问题是我应该如何发布此对象?

在下面的代码中,您会看到我将removeFromSuperview发送到loadingScreen,但我仍然拥有此对象的所有权,除非我发布它。但是,如果我发布它,那么viewdidUnloaddealloc中就没有任何内容可以发布。

- (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
}

1 个答案:

答案 0 :(得分:2)

当您释放loadingScreen时,将其重置为零值。

[loadingScreen release];
loadingScreen = nil;

[nil release]不会发生任何事情。