Skip to content

Instantly share code, notes, and snippets.

@mteece
Created December 20, 2012 15:17
Show Gist options
  • Select an option

  • Save mteece/4345871 to your computer and use it in GitHub Desktop.

Select an option

Save mteece/4345871 to your computer and use it in GitHub Desktop.
Removing subviews from a view by tag.
// Each UIView object has a tag property which is simply an integer value
// that can be used to identify that view. You could set the tag value of
// each added view to 7 like this. The use the for loop to clean up the
// subview if needed.
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:self.view];
CGRect myImageRect = CGRectMake((touchPoint.x -50), (touchPoint.y -50), 80.0f, 90.0f);
UIImageView *myImage = [[UIImageView alloc] initWithFrame:myImageRect];
[myImage setImage:[UIImage imageNamed:@"button"]];
myImage.opaque = YES; // explicitly opaque for performance
myImage.tag = 999;
....
// Clear the subview by tag 999.
for (UIView *subview in [self.view subviews]) {
if (subview.tag == 999) {
[subview removeFromSuperview];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment