Daniel Granger
Daniel Granger

Reputation: 1334

Add and removing a unspecified number of UIImageviews programmatically

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?

Upvotes: 1

Views: 445

Answers (1)

brutella
brutella

Reputation: 1617

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--;

Upvotes: 1

Related Questions