1

我正在创建一个加载屏幕 UIView,它被添加到子视图中,同时从某个 URL 解析一些 XML。一旦返回 XML,加载屏幕就会从其父视图中移除。

我的问题是我应该如何释放这个对象?

在下面的代码中,您会看到我发送removeFromSuperview到 loadingScreen,但除非我释放它,否则我仍然拥有该对象的所有权。但是,如果我释放它,在viewdidUnloadand中将没有任何东西可以释放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
}
4

1 回答 1

2

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

[loadingScreen release];
loadingScreen = nil;

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

于 2010-09-11T14:12:03.760 回答