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 Answer 1

1

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--;
Sign up to request clarification or add additional context in comments.

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.