I have found that assigning a tag for each subview I create allows me to remove it at some future point before the superview is removed, makes it simple to bring in and out elements according to their tag value.
Every object on a view is a subview, so keeping tags on them seems sensible for some applications when graphics are used in a simple way.
Lets say you want to draw a simple horizontal line:
part of a function to draw a horizontal line:
CGFloat lineWidth=8;
UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(oxx,oyy-(lineWidth/2),pxx+(lineWidth/2),pyy)];
lineView.backgroundColor = colours;
lineView.tag=5;
[self.view addSubview: lineView]; ...
then some time later, to remove some tagged subviews:
for (UIView *subview in [self.view subviews]) {
if (subview.tag == 101 || subview.tag == 102 ||subview.tag == 103 ||subview.tag == 104 || subview.tag == 5) {
[subview removeFromSuperview];
}
}