I’m trying to remove a UIView that is initialised when a button is clicked. I’ve clicked that button twice, so two of the same UIView appear. I can remove the first one no problem but when I try to do that same thing with the second of the same view nothing happens? I figure this is because it’s a duplicate it doesn’t register that it is there as it thinks its been removed…
Here is my code:
- (void)createStamp13 {
//--EDIT--//
stampV13Array = [[NSMutableArray alloc] initWithCapacity:3];
[stampV13Array addObject:stampV13];
stampV13 = [[UIView alloc] initWithFrame:CGRectMake(200, 300, 172, 330)];
//[firstPage addSubview:stampV13];
[firstPage addSubview:(UIView*)[stampV13Array objectAtIndex:0];
//--//
stampV13.backgroundColor = [UIColor clearColor];
stampThirteen = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"exclaim_r.png"]];
stampThirteen.frame = CGRectMake(0, 0, 172, 330);
[stampV13 addSubview:stampThirteen];
}
that is the button function for when I create it. This is the remove function:
- (void)removeImage5:(id)sender{
NSLog(@"Delete StampV13");
if (stampV13) {
[self.stampV13 removeFromSuperview];
}
}
I hope that what I explained makes sense, I’d appreciate any help! Thanks.
stampV13is a single variable and can only point to one object at a time. If you callcreateStamp13twice, the variable has lost track of the first object it pointed to. To have multiple copies of these views, keep their references in an array instead of one variable and do whatever you need with all the array elements.Some additional details:
@property (strong, nonatomic) NSMutableArray *stampV13Array;and synthesize it.stampV13Arrayinside an ‘init…’ method for your class that contains thecreateStamp13code. (Which exact method depends on how you crate objects of that class.)createStamp13method to beUIView *stampV13 = [[UIView alloc] initWithFrame:CGRectMake(200, 300, 172, 330)];.[self.stampV13Array addObject:stampV13];.After that you’ll need to reference members of the array wherever you used to use
stampV13.