iPhone SDK 管理多视图应用程序的内存

发布于 2024-09-16 23:20:11 字数 173 浏览 2 评论 0原文

我有一个应用程序,可以根据文本文件的内容以编程方式创建标签和文本字段。每当加载视图控制器时,它都会创建每次都不同的文本字段和标签。我的问题是我需要清除标签和文本字段而不释放视图控制器,因为我需要跟踪视图控制器。我尝试了 self.viewController = nil,但我很确定这会导致内存泄漏。有没有办法删除视图的所有子视图?

I have an app that programmatically creates labels and text fields based on the contents of a text file. Whenever the view controller is loaded, it creates text fields and labels that are different each time. My problem is I need to clear out the labels and text fields without releasing the view controller since I need to keep track of the view controller. I tried self.viewController = nil, but I'm pretty sure this results in a memory leak. Is there a way to delete all the subviews of a view?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

夜无邪 2024-09-23 23:20:11

Greg 的意思是这样的:

for (UIView *subview in self.view.subviews) {
    [subview removeFromSuperview];
}

但这可能不会像您期望的那样工作,因为 Objective C 不喜欢在 for 循环中迭代数组时修改数组。更安全的选择是这样的:

while ([self.view.subviews count] > 0) { 
    [[self.view.subviews lastObject] removeFromSuperview]; 
}

What Greg meant to say is this:

for (UIView *subview in self.view.subviews) {
    [subview removeFromSuperview];
}

This may not work as you would expect though, as Objective C doesn't like it when you modify an array while you're iterating through it in a for loop. A safer choice would be this:

while ([self.view.subviews count] > 0) { 
    [[self.view.subviews lastObject] removeFromSuperview]; 
}
朱染 2024-09-23 23:20:11

如果您有一个名为 view 的视图,您应该能够使用以下代码从视图中删除所有子视图:

for (UIView *subview in view) {
    [subview removeFromSuperview];
}

If you have a view named view, you should be able to remove all of the subviews from the view using this code:

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