何时释放从超级视图中删除的 UIView

发布于 2024-09-18 21:56:11 字数 1012 浏览 4 评论 0原文

我正在创建一个加载屏幕 UIView,它被添加到子视图中,同时从某个 URL 解析一些 XML。返回 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
}

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绝影如岚 2024-09-25 21:56:11

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

[loadingScreen release];
loadingScreen = nil;

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

When you release loadingScreen, reset it to a nil value.

[loadingScreen release];
loadingScreen = nil;

[nil release] won't happen anything.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文