简体   繁体   中英

Remove from superview

Just a small question i'm really having a lot of problems with

Basically, what I'm doing is making a view every time I hit a button which works fine. When I want to remove all the images I made when I hit the remove from superview it just removes the last one on the stack. Is there a way i can get rid of all the images I made?

Here is the code

This puts the picture on the screen

- (IBAction)pushBn:(id)sender {


    ZeldaView *newZelda = [[ZeldaView alloc]initWithNibName:@"ZeldaView" bundle:nil];

    theZeldaView=newZelda;
    [self.view insertSubview:theZeldaView.view atIndex:1];

}

this removes it when i touch it

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{


    UITouch *touch =[touches anyObject];
    CGPoint location=[touch locationInView:theZeldaView.view];

    if (CGRectContainsPoint(theZeldaView.theImage.frame, location)) {

        [theZeldaView.view removeFromSuperview];

    }

}

Sure, just iterate through the children of your parent view (self.view) and remove any that are ZeldaView elements.

At it's simplest this would be something like:

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

Though you will probably want to expand on this to not perform the actual removal during the iteration and you may want to use respondsToSelector and a custom method instead of checking the class here so that you can do any cleanup needed from within the ZeldaView class. Hopefully that makes sense to you.

sure, many ways to do this. I would keep an array of ZeldaView objects and when you want to remove them, traverse the array and remove them all.

in your .h:

@property (nonatomic, retain) NSMutableArray *zeldaViews;

when you add a ZeldaView:

// create a newZeldaView and add it to the superview
[self.zeldaViews addObject:newZeldaView];

when you want to remove them all:

for (ZeldaView *zeldaView in self.zeldaViews) {
    [zeldaView.view removeFromSuperview];
}
[self.zeldaViews removeAllObjects];

create the mutable array in viewDidLoad and release it in viewDidUnload and dealloc. Modify as approp if your using ARC.

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