Swift:当我转向另一个视图控制器时,内存未清除,收到内存警告 [英] Swift: Memory not clearing when I segue to another view controller, recieving memory warning
问题描述
我正在为iOS 8.1在Xcode 6.2中构建一个应用程序。我有一个 UIViewController
( LevelViewController
),其中包含 UICollectionView
其中的每个单元代表我游戏中的一个级别(每个单元都有一个带有数字的标签)。选择其中一个单元格后,我执行show segue到 SKScene
,它会加载该特定级别的所有数据。我还有一个菜单按钮,用于执行一个节目segue回到主菜单。
I am building an application in Xcode 6.2, for iOS 8.1. I have a UIViewController
(LevelViewController
) which contains a UICollectionView
whose cells each represent a level in my game (each one has a label w/ a number). Once one of the cells is selected I perform a show segue to an SKScene
which loads all the data for that particular level.I also have a "menu" button which performs a show segue back to the main menu.
功能上这一切都有效,但是在执行两个segues后我遇到了严重的内存问题。在仪器中窥视后,当我从 LevelViewController
中删除我为每个添加的所有 UILabel
时个人 UICollectionViewCell
保留在内存中,以及单元格中包含的所有其他内容。应该只有192个标签(对于192个级别)但是在执行这个segue几次后,它们在乐器中总计大约1000个。
Functionally this all works, however I am having serious memory problems after performing both segues. After peeking in instruments it appears that when I segue out of the LevelViewController
that all of the UILabel
that I added for each individual UICollectionViewCell
remain in memory, along with everything else contained in the cells. There should only be 192 labels (for 192 levels) but after performing this segue several times they add up to around 1000 in instruments.
显然这些并没有在记忆中被释放,我的理解是swift应该照顾它,所以我不确定问题是什么。我还应该注意到 UICollectionView
是以编程方式添加的,并且没有使用 IBOutlets
。
Obviously these are not being deallocated in memory, It's my understanding that swift should take care of that, so i'm not sure what the problem is. I should also note that the UICollectionView
was added programmatically, and no IBOutlets
are used.
那么当我离开那些标签时,我究竟能如何摆脱这些标签,实际上, UICollectionView
本身 LevelViewController
。我对此非常困惑,这毁了我的圣帕特里克节。因此,爱上所有爱的人请帮助一个小伙子:)
So how exactly can I get rid of those labels, and really, the UICollectionView
itself when I segue away from the LevelViewController
. Im seriously confused about this and it's ruining my St. Patricks Day. So for the love of all things Irish please help a lad out :)
注意:我尝试过的方法
self.collectionView.removeFromSuperView()
self.collectionView = nil
self.collectionView.deleteItemsAtIndexPaths(path)
推荐答案
如果没有看到用于执行这些segues的代码,很难说清楚。但我猜这个问题与你如何在视图控制器之间产生分歧有关。
It's hard to tell without seeing the code you're using to perform these segues. But I'm guessing that the problem has to do with how you are segueing between the view controllers.
我还有一个菜单按钮,可以执行show segue返回主菜单。
I also have a "menu" button which performs a show segue back to the main menu.
如果我不得不根据你上面的语言猜测,而不是弹出Level ViewController(我假设它嵌入在 UINavigationController中
),你试图使用 performSegueWithIdentifier:
回到主菜单这实际上会创建一个视图控制器的新实例并将其推送到导航堆栈(并保留它的现有实例,导致你的内存困境)。
If I had to guess based on your language above, instead of popping the Level ViewController (I'm assuming it is embedded in a UINavigationController
), you are trying to segue back to the main menu using performSegueWithIdentifier:
This will actually create a new instance of the view controller and push it onto the navigation stack (and retain the existing instance of it leading to your memory woes).
如果这确实是您的问题,解决方案非常简单:当点击您的菜单按钮时,您应该调用 popViewControllerAnimated
或 dismissViewControllerAnimated:completion:
。
If that's indeed your problem, the solution is pretty simple: When your menu button is tapped, you should be calling popViewControllerAnimated
or dismissViewControllerAnimated:completion:
.
当你习惯于使用iOS segueing时,一个值得做的简单的事情是向所有视图控制器添加去初始化器,如下所示,以获得对象的持续调试消息正在按预期取消分配:
A simple little thing worth doing while you get accustomed to iOS segueing is to add de-initializers to all your view controllers as follows so as to get ongoing debug messages that objects are being deallocated as expected:
deinit {
debugPrintln("Name_of_view_controlled deinitialized...")
}
Happy St. Paddy's Day!
Happy St. Paddy's Day!
这篇关于Swift:当我转向另一个视图控制器时,内存未清除,收到内存警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!