remove a programmatically added UIImageView

iphone, removeall, uiimageview, xcode

Solution

Set a `tag` for your image view & then you can get it by this tag.

[img setTag:123];

...

[[self.view viewWithTag:123] removeFromSuperview];

Problem

i'm making a program that has 2 Buttons in main view ; one is called show and other one is hide, when user presses show butoon an imageview gets added to screen code : ``` -(IBAction)show{ UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 155, 155)]; img.image = [UIImage imageNamed:@"icon.png"]; [self.view addSubview:img]; } ``` and when user presses the hide button , i want app to hide the image that has been just added (img) but... when i use ``` -(IBAction)add{ [img removeFromSuperView]; } ``` Xcode says "img Undecleared" edit : Some said define the object as a public object (@property) but problem is that the imageview gets added just once. but i wanted it to add new imageview every time user presses the Show button, so i used the [[self subviews]objectAtIndex:xx]removeFromSuperview] method to solve the problem

Original source