I’m trying to remove all Sprites (UIImageViews) on my screen with the following code:
-(IBAction)clearAll:(id)sender{
for (Sprite *sprite in self.view.subviews){
[sprite removeFromSuperview];
}
However, when this code runs, elements from my Storyboard which are NOT Sprites are removed. Actually, everything in the view is pretty much removed.
What is going on?
This isn’t how for in loops work. Just because you’ve specified a type, doesn’t mean that only objects of that type will be affected. Every view in
subviewsresponds toremoveFromSuperview, so regardless of what it has been cast as, it’ll still be removed.If you want to remove only
Spriteobjects, then you need to check the class of each object.