views:

17

answers:

1

My app is a standard casino game where you bet casino chips. When the user taps a chip that chip gets added to the chip pile being used for the bet. I do this by adding a UIImageView on top of (slightly offset to give the appearance of a stack of chips) the other chips (also uiimageviews).

UIImageView *addChip = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"chip"]];
addChip.opaque = YES;
addChip.frame = CGRectMake(131, 268, 57, 57);
[self.view addSubview:addChip];
[addChip release];

This pile of chips can obviously be any number UIImageViews (depending on how many chips the player puts down). When the user wants to remove a chip from the pile or the player loses their bet how do I know which subviews to remove?

+1  A: 

You can access an image afterwards through the tag property. For example: you index the chips

int numberOfCoins = 0; 

//add new coin
UIImageView *addChip =  ... 
addChip.tag = numberOfCoins; 
[self.view addSubview:addChip]

numberOfCoins++;

The next time you add a coin, you can do it the same way. If you want to remove the last coin you can access the image view with the tag and remove it

[[self.view viewWithTag:numberOfCoins] removeFromSuperview];
numberOfCoins--;
brutella
I tried this but if you use an already assigned tag (by interface builder) such as 0 it removes both things. So to stop this getting messy I just added the imageview to a mutable array and use that instead
Daniel Granger
that's another way, right.
brutella