Scenario 1:
For a UIViewController, is it better to (1) create an ivar for a UIView that I access again in 1 or 2 functions outside of loadView
? Or, (2) should I just tag it in loadView
and then use - (UIView *)viewWithTag:(NSInteger)tag
to access it again in the other functions? I'm guessing that option 1 increases the memory by the size of a pointer, so 32/64-bits, and creates accessor methods (assuming I declare @property
& @synthesize
), and then requires releasing the ivar in dealloc
and setting it to nil
in viewDidUnload
... and that option 2 saves memory, has less setup code, but costs some processing time and a little extra code to find the view by its tag. Am I right about all this?
In this scenario, it feels best to use an ivar, but I'm not sure.
Scenario 2:
What about for a custom subclass of UIView that has 5 subviews? Bearing in mind that I'll have about 30 instances of this custom subclass in memory at a given time (They'll be subviews of tableViewCell
s.), should I use 5 ivars for the subviews, or should I tag them all?
In this scenario, I'm thinking the memory saved by tagging them all would be worth the small performance hit of searching for them with - (UIView *)viewWithTag:(NSInteger)tag
.
Thoughts?
Thanks!
Matt