Reputation: 797
I have always wondered what are the best practices while working with UIImageView
objects and a will give you a few examples about what I'm unsure.
First of all I am working on a turn based game that supports multiple matches at the same time, and certain views (a background image, label and a few buttons) will be loaded to self.view
very often. What is the best way to display them, add them and then remove:
[self.view addSubview:view];
[view removeFromSuperView];
Or is the best way to add them and play with the hidden property (show and hide whenever i need, even in different matches)?
Another question is do I need to set an UIImageView
to nil
after I remove it from superview?
And the last question is: If I have a UIView
class that I load to an UIImageViewController
and want to release/remove it from within [self removeFromSuperView]
is enough to release all the memory occupied by that view class?
Upvotes: 1
Views: 182
Reputation: 5616
Regarding points 2 and 3, it really depends on the scope of your UIImageView
. If you declared it as a strong
property, then you will have to set it to nil in order for ARC
to release the memory. If it just a variable inside a method, then at the end of the execution of the method body, the variable will be released anyway (and be retained only by the view hierarchy).
Upvotes: 1
Reputation: 20410
If those views are loaded often into the screen, the best approach is to hide them instead of removing them. I'll remove them when I'm not using them anymore.
Removing a view from the superview reduces the retain count of the object in 1. If you are using ARC, you shouldn't worry about it, if you are not, be sure that the retain count is 0 after removing it (+1 for every alloc, add to subview, and -1 for every release, autorelease, removeFromSuperView). If after removing the view the retain count still 1, you can do = nil to release it. If the retain count of an object is 0, then the system will free it.
Same as 2.
Upvotes: 1