views:

38

answers:

2

Well this has been bugging me for quite a while so now i'll ask

When should uiviewcontrollers be declared as properties? I noticed that in many examples uiviewcontrollers that are being pushed and popped on a uinavigation controller are not declared as properties.

I took that forward and now every uiviewcontroller i use inside my code is not declared as a property (ofc memory is handled carefully), even my main viewcontroller inside the appdelegate. However since instrument was reporting a leak i tried to declare it as a property... the result is that, even if the leak is still somewhere, memory occupation of the app has been halved.

So now i'm wondering... is my approach completely wrong and all uiviewcontrollers should be declared as properies? is there a particular reason to declare a uiviewcontroller as a property or not?

thanks for any tips on this matter

A: 

View controller views can take up a lot of memory, and the iPhone doesn't have a lot of memory to spare. Maybe there is some part of the controller that you need, but I wouldn't retain one unless it absolutely needed to be kept around after it is dismissed.

Alex Reynolds
Yeah but when should they be declared as a property and when not?
koda
A: 

You'd only want to hold a reference to the UIViewController if you needed to access it again from the view controller you pushed it to. You don't really want to hold it in memory all the time in any other situation, and consider "lazy loading" all your view controllers instead (probably what you were doing beforehand) and releasing them immediately (UINavigationController holds a retained reference to it, so your app won't crash/release it while it is on-screen).

What was the code which Instruments said was leaking? Please post.

Helen
instruments was reporting the warning right at the beginning in the appdelegate (`[uiwindow addSubview:mainViewController.view];`) but that was with the simulator and now that i can test on device it doesn't print anything.the code i'm referring at is more like fromt this file: http://code.google.com/p/hedgewars/source/browse/project_files/HedgewarsMobile/Classes/MainMenuViewController.mthe two viewcontrollers need to be instance variables because they are used in SwitchView but they are not properties. should they be?
koda