UIButtons ,which are dynamic, not changeing its background image properly as per selected button's tag.
<p>-(void)setButtons_AsPerTheMatrixSelection</p>

<p>{</p>

<pre><code>    int x = 7;
    int y = 60;

    for (int j = 0; j &lt;= 35; ++j)
    {
        if (j &lt;= 5)
            btnMatrix = [[UIButton alloc] initWithFrame:CGRectMake(x, y, width, height)];
        else if (j &gt; 5  &amp;&amp; j &lt;= 11)
            btnMatrix = [[UIButton alloc] initWithFrame:CGRectMake(x-306, y+51, width, height)];
        else if (j &gt; 11  &amp;&amp; j &lt;= 17)
            btnMatrix = [[UIButton alloc] initWithFrame:CGRectMake(x-612, y+102, width, height)];
        else if (j &gt; 17  &amp;&amp; j &lt;= 23)
            btnMatrix = [[UIButton alloc] initWithFrame:CGRectMake(x-918, y+153, width, height)];
        else if (j &gt; 23  &amp;&amp; j &lt;= 29)    
            btnMatrix = [[UIButton alloc] initWithFrame:CGRectMake(x-1224, y+204, width, height)];
        else if (j &gt; 29  &amp;&amp; j &lt;= 35)
            btnMatrix = [[UIButton alloc] initWithFrame:CGRectMake(x-1530, y+255, width, height)];

        btnMatrix.tag = j;
        [btnMatrix setBackgroundImage:imgDefaultBG forState:UIControlStateNormal];
        btnMatrix.userInteractionEnabled = TRUE;
        [btnMatrix addTarget:self action:@selector(changeImage:) forControlEvents:UIControlEventTouchUpInside];

        [viewWithButtons addSubview:btnMatrix];
        [self.view addSubview:viewWithButtons];

        x = x + 51;
        y = y;
        [arrButton addObject:btnMatrix];
    }
</code></pre>

<p>}</p>

<p>This is my code to add dynamic buttons as per the matrix of 6*6</p>

<p>here i am adding the button tag also,</p>

<p>Now i am going to click on button &amp; would like to change background image with this code...</p>

<p>-(void)changeImage:(id)sender</p>

<p>{</p>

<pre><code>UIImage *img = [UIImage imageNamed:@"No.png"];

UIButton *tmpButton = (UIButton *)[self.view viewWithTag:btnMatrix.tag];

NSLog(@"btn.tag is... %d",tmpButton.tag);


[btnMatrix setBackgroundImage:img forState:UIControlStateNormal];
</code></pre>

<p>}</p>

<p>When i click to any button it always changing image of last tagged button...</p>

<p>i have to change Background of the selected button itself.</p>

<p>how can it be possible.</p>

<p>Please guide me i am not getting a part where i am doing a mistake.</p>

<p>Thanks.</p>
