Who to access a UIButton using tag and button change image?

Posted by ste on Stack Overflow See other posts from Stack Overflow or by ste
Published on 2010-03-18T17:57:33Z Indexed on 2010/03/18 18:01 UTC
Read the original article Hit count: 277

Filed under:
|
|

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?

© Stack Overflow or respective owner

Related posts about iphone

Related posts about uibutton