views:

82

answers:

0

Hello everyone,

I have a NSMutablArray of UIButtons. I want to index through them and set their images based on other data. The problem I'm having is finding a loop to pass them all through (I don't want a long list of ifs).

for (UIButton *theButton in theButtons) {

A loop will not work. What other way is there to be able to set images for my buttons in my array?

this is what i got now. this is the only time this array is used

// "theGrid[x][y]" is declared in the app delegate  and i do have the #import
// 
    theButtons = [NSMutableArray arrayWithObjects:space1,space2,space3,space4,space5,space6,space7,space8,space9,nil];


UIImage *xImage = [UIImage imageNamed:@"X.png"];
UIImage *oImage = [UIImage imageNamed:@"O.png"];
UIImage *blankImage = [UIImage imageNamed:@"blank.png"];

int row = 1;
int col = 1;

for (UIButton *theButton in theButtons) {
// since i have 9 buttons i dont want this if statement repeated 9 times for each button
    if (theGrid[row][col] == 1) {
        [theButton setImage:xImage forState:UIControlStateNormal];// i believe this line is the error
    }
    else if (theGrid[row][col] == 2) {
        [theButton setImage:oImage forState:UIControlStateNormal];
    }
    else {
        [theButton setImage:blankImage forState:UIControlStateNormal];
    }

    row++;
    if (row >= 3) {
        row = 1;
        col++;
    }
}

Thanks.