-(void)setButtons_AsPerTheMatrixSelection
{
int x = 7;
int y = 60;
for (int j = 0; j <= 35; ++j)
{
if (j <= 5)
btnMatrix = [[UIButton alloc] initWithFrame:CGRectMake(x, y, width, height)];
else if (j > 5 && j <= 11)
btnMatrix = [[UIButton alloc] initWithFrame:CGRectMake(x-306, y+51, width, height)];
else if (j > 11 && j <= 17)
btnMatrix = [[UIButton alloc] initWithFrame:CGRectMake(x-612, y+102, width, height)];
else if (j > 17 && j <= 23)
btnMatrix = [[UIButton alloc] initWithFrame:CGRectMake(x-918, y+153, width, height)];
else if (j > 23 && j <= 29)
btnMatrix = [[UIButton alloc] initWithFrame:CGRectMake(x-1224, y+204, width, height)];
else if (j > 29 && j <= 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];
}
}
This is my code to add dynamic buttons as per the matrix of 6*6
here i am adding the button tag also,
Now i am going to click on button & would like to change background image with this code…
-(void)changeImage:(id)sender
{
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];
}
When i click to any button it always changing image of last tagged button…
i have to change Background of the selected button itself.
how can it be possible.
Please guide me i am not getting a part where i am doing a mistake.
Thanks.
BTW, your code is too messy, you may do like this