-(void)setButtons_AsPerTheMatrixSelection
{
for(UIView *subview in [viewWithButtons subviews])
{
if ([subview isKindOfClass:[UIButton class]])
{
[subview removeFromSuperview];
}
}
viewWithButtons = [[UIView alloc] init];
width = 48;
height = 48;
pw = 49;
ph = 49;
arrButton = [[NSMutableArray alloc] init];
UIImage *imgDefaultBG = [UIImage imageNamed:@"bg.jpg"];
viewWithButtons.frame = CGRectMake(50, 40, 200, 260);
ch = 4;
cv = 4;
for ( i = 0 ; i < cv ; ++i )
{
for ( j = 0 ; j < ch ; ++j )
{
btnMatrix = [[[UIButton alloc] initWithFrame:CGRectMake(10+pw*j, 51+ph*i, width, height)] autorelease];
btnMatrix.tag = i*ch+j;
btnMatrix.userInteractionEnabled = TRUE;
bulImageStatus = FALSE;
[btnMatrix addTarget:self action:@selector(changeImage:) forControlEvents:UIControlEventTouchDown];
[btnMatrix setBackgroundImage:imgDefaultBG forState:UIControlStateNormal];
[viewWithButtons addSubview:btnMatrix];
[arrButton addObject:btnMatrix];
}
}
NSLog(@"arrButton object count is:--> %d",[arrButton count]);
[self.view addSubview:viewWithButtons];
}
-(void)AddImageToArray
{
arr_FirstSet = [[NSMutableArray alloc] init];
NSString *strImageName;
if(appDelegate.intCategoryBtnTag == 0)
{
for (intimg = 1; intimg <= 28; intimg++)
{
strImageName = [NSString stringWithFormat:@"%d_1.png",intimg];
NSLog(@"strImageName is :--> %@",strImageName);
[arr_FirstSet addObject:strImageName];
}
NSLog(@"arr_FirstSet objects are...%@",arr_FirstSet);
}
}
-(void)changeImage:(id)sender
{
UIImage *img;
NSString *strImageName;
strImageName = [arr_FirstSet objectAtIndex:arc4random() % [arr_FirstSet count]/2];
NSLog(@"btnMatrix is:--> %@",strImageName);
img = [UIImage imageNamed:strImageName];
//[btnMatrix setImage:img forState:UIControlEventTouchDown];
NSLog(@"sender detail is:--> %@",sender);
[sender setBackgroundImage:img forState:UIControlStateHighlighted];
}
这是我在“ setButtons_AsPerTheMatrixSelection ”方法中设置动态按钮的代码,
“ AddImageToArray ”方法用于将bundle中的图像逐个添加到NSMutableArray(arr_FirstSet)中。
“ changeImage ”方法用于设置特定按钮的背景。
我可以将图像随机设置为按钮的背景,
但主要问题是我必须为特定按钮设置固定的动态图像。
现在,每次单击特定按钮时,当我按一次、两次、三次等时,我都会更改随机图像...
我必须将在“changeImage”中随机生成的任何特定图像设置为单个按钮,并将其他按钮设置为单个。
然后我必须检查两个按钮是否具有相同的背景,然后这两个按钮将从矩阵中删除。
请指导我犯的错误并帮助我。