简体   繁体   中英

UIImage in array > how to remove it

I have an array of UIImages that I show in UIViews like this

UIImage *image=[self.currentAlphabet objectAtIndex:i ];
UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(xPos, yPos, imageWidth, imageHeight)];
imageView.image=image;
[self.view addSubview:imageView];

now later I want to remove these images from the view. I thought it should work like this

UIImage *image=[self.currentAlphabet objectAtIndex:i ];
UIImageView *imageView=[[UIImageView alloc]initWithImage:image];
[imageView removeFromSuperview];

but it doesn't work like that... do I need to save the UIImageViews in the array or is there any solution that needs less changes in the code written already?

You are creating a brand new view object (which isn't in the hierarchy) and then removing it from the view hierarchy. It doesn't work for this reason.

You have to remove the view object that you've previously added to view hierarchy (in the first snippet).

Basically, you have to keep track of those views: store them also in an NSMutableArray , use the tag property of UIView or use an instance variable. It depends by how many views you have to add to your hierarchy. If you have a few of them, an instance variable is fine. If you have many of them, use an NSMutableArray .

For example, using a secondary NSMutableArray :

UIImage *image = [self.currentAlphabet objectAtIndex:i];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(xPos, yPos, imageWidth, imageHeight)];
imageView.image = image;
[self.view addSubview:imageView];
[imageViewArray addObject:imageView];

later on (I suppose that at this point you know the index of the object to remove):

[imageViewArray removeObjectAtIndex:i]

As long as the only image views in your view are the ones you want removed, a simple method to remove them is the following:

for (UIView *view in self.view.subviews) {
    if ([view isKindOfClass:[UIImageView class]]) {
        [view removeFromSuperview];
    }
}

What you are doing is creating a new Instance of UIImageView and without even Adding it to your view heirarchy you are removing it.

UIImage *image=[self.currentAlphabet objectAtIndex:i ];
UIImageView *imageView=[[UIImageView alloc]initWithImage:image];// It's a fresh new ImageView
[imageView removeFromSuperview];

What you can do is...

1) If there is only one ImageView in your View.. Then Create a Class Variable in .h File for UIImageView..

UIImageView *yourImageView; // in .h file

yourImageView.image = //Set Image here.

yourImageView.image = nil; // For removing the image.

2) Or If you don't want to create a Class variable for that.. Just give tag to your ImageVIew... And access it with viewWithTag method of UIView.

yourImageView.tag = 2;

and access it like following.

UIImageView *imageViewRef =  [self.view viewWithTag:2];
[imageViewRef removeFromSuperView];

Hope this will help you..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM