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.
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.
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.
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 :)
Note: methods I have tried
self.collectionView.removeFromSuperView()
self.collectionView = nil
self.collectionView.deleteItemsAtIndexPaths(path)
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.
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 usingperformSegueWithIdentifier:
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).If that's indeed your problem, the solution is pretty simple: When your menu button is tapped, you should be calling
popViewControllerAnimated
ordismissViewControllerAnimated:completion:
.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:
Happy St. Paddy's Day!