views:

62

answers:

1

I have created an array of UIButtons. I need to change the images on those buttons programmatic, at various times. For time being, in order to get this to work, I want to change the image when the button is pressed, but this is not the time this would really happen.

So, I create an array of buttons, and arrange them on a grid on the screen:

for (UInt16 index=0; index < (mNumColumns * mNumRows); index++)
{
<....Snipped code for setting the x,y,width,height vars...>
UIButton *thisCell = [UIButton buttonWithType:UIButtonTypeCustom];
thisCell.tag = index;
thisCell.frame = CGRectMake(currentX, currentY, cellWidth, cellHeight);

[thisCell addTarget:self action:@selector(buttonPushed:)
forControlEvents:UIControlEventTouchUpInside];
[thisCell setImage:[UIImage imageNamed:@"cellBackground.png"] forState:UIControlStateNormal];
[[self view] addSubview:thisCell];
}

---And then the button handler--

-(void)buttonPushed:(UIView*)clickedButton
{
NSLog(@"Click %d", clickedButton.tag);
[clickedButton setImage:[UIImage imageNamed:@"cellEmpty.png"] forState:UIControlStateNormal];
[clickedButton setNeedsDisplay];

}


1) The initial grid of buttons displays correctly.
2) If I change the filename of the PNG for the intial grid to cellEmpty.png, it works with the new image, so I know the image could display.
3) I see the "Click n" in the debugger, so the correct button is calling the buttonPushed

But, as you can guess, the image is not getting updated. I do not think I want to deal with button states at this time....since the image changing does not really have to do with the button getting pressend in real life (the change would be result of downloading a new image over the network).

A: 

Thanks for your help. There is something else interesting going on here that I need to figure out. The view that is creating the buttons is a frame on a tab bar, and if I change tab bars to another view and come back, the images have updated.

I will update the answer when I figure it out.

Brett