views:

316

answers:

1

I need to change the images on a matrix of UIButtons, and the only thing I know of to address the buttons, is the tag. But I can not find a way to actually us this identifier. The buttons are created programmatically during viewDidLoad.

Here is the code for creating the buttons:

#define N_ROWS  4
#define N_COLS  3

    int N_IMG = 0;
    for (int a = 0; a < N_COLS; a++) {
        for (int j = 0; j < N_ROWS; j++) {


            UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
            aButton.frame = CGRectMake(a * 65.0 + 25, j * 65.0 + 15, 10.0, 10.0);
            aButton.tag = j + a * N_ROWS + 1;
            [aButton setBackgroundColor:[UIColor redColor]];

            N_IMG = N_IMG++;
            [self.view addSubview:aButton]; 
            number_sorted =  1;

        }
    }

Here is the code for setting the image:

- (IBAction)set_image:(id)sender {

    #define N_ROWS  4
    #define N_COLS  3

        int N_IMG = 0;
        for (int a = 0; a < N_COLS; a++) {
            for (int j = 0; j < N_ROWS; j++) {
                uibutton aButton.tag == (j + a * N_ROWS + 1) 
                setImage:[UIImage imageNamed:[puzzles objectAtIndex:N_IMG]]
                            forState:UIControlStateNormal];
            N_IMG = N_IMG++;

            }
        }
    }

This is the code where the truble starts: uibutton aButton.tag == (j + a * N_ROWS + 1)

Who can I set this up to work?

+3  A: 

I don't really understand what you are trying to do, but why can't you store your UIButton objects (ie in an NSArray object), so you can access them later (in your second loop)?

Macmade