xcode Removing Some Subviews from view
Asked Answered
C

2

13

Greetings all,

I am a noob and I have been trying to work through this for a few days.

I am adding images to a view via UItouch. The view contains a background on top of which the new images are add. How do I clear the images I am adding from the subview, without getting rid of the UIImage that is the background. Any assistance is greatly appreciated. Thanks in Advance.

here is the code:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *) event { 
NSUInteger numTaps = [[touches anyObject] tapCount];

if (numTaps==2) {
    imageCounter.text =@"two taps registered";      

//__ remove images  
    UIView* subview;
    while ((subview = [[self.view subviews] lastObject]) != nil)
        [subview removeFromSuperview];

    return;

}else {

    UITouch *touch = [touches anyObject];
    CGPoint touchPoint = [touch locationInView:self.view];
    CGRect myImageRect = CGRectMake((touchPoint.x -40), (touchPoint.y -45), 80.0f, 90.0f); 
    UIImageView *myImage = [[UIImageView alloc] initWithFrame:myImageRect];

    [myImage setImage:[UIImage imageNamed:@"pg6_dog_button.png"]];
     myImage.opaque = YES; // explicitly opaque for performance


    [self.view addSubview:myImage];
    [myImage release];

    [imagesArray addObject:myImage];

    NSNumber *arrayCount =[self.view.subviews count];
    viewArrayCount.text =[NSString stringWithFormat:@"%d",arrayCount];
    imageCount=imageCount++;
    imageCounter.text =[NSString stringWithFormat:@"%d",imageCount];

}

}

Chopstick answered 9/11, 2010 at 17:40 Comment(0)
F
29

What you need is a way of distinguishing the added UIImageView objects from the background UIImageView. There are two ways I can think of to do this.

Approach 1: Assign added UIImageView objects a special tag value

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:

myImage.tag = 7;

Then, to remove the added views, you could step through all of the subviews and only remove the ones with a tag value of 7:

for (UIView *subview in [self.view subviews]) {
    if (subview.tag == 7) {
        [subview removeFromSuperview];
    }
}

Approach 2: Remember the background view

Another approach is to keep a reference to the background view so you can distinguish it from the added views. Make an IBOutlet for the background UIImageView and assign it the usual way in Interface Builder. Then, before removing a subview, just make sure it's not the background view.

for (UIView *subview in [self.view subviews]) {
    if (subview != self.backgroundImageView) {
        [subview removeFromSuperview];
    }
}
Fishgig answered 9/11, 2010 at 18:4 Comment(3)
Answer 1 is generally correct but note that you could restrict the search using [UIView viewWithTag: 7] and not have to iterate through every single view. Of course, this might not save you anything, but it is a nice thing to knowFoulard
Guys thanks so much for your input. I will try these out today. I did a long work around which was to add the background back after everything had been cleared. Not exactly elegant but it worked..Chopstick
Your workaround is very clever, though it might not work so well when you need more control over your views. If you try using viewWithTag:, as suggested by @justin, keep in mind that it will only return the first view found with a matching tag. It's a very convenient method when there's only one such view. If you have lots of views with matching tags, you'll end up iterating to find them all. Extra tip: Views have a default tag value of zero, so avoid using zero as an identifying tag value.Fishgig
I
0

A more swiftly code for approach #1 in only one functional line of code :

self.view.subviews.filter({$0.tag == 7}).forEach({$0.removeFromSuperview()})
Implant answered 3/10, 2016 at 13:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.