-(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」メソッドは、バンドルからNSMutableArray(arr_FirstSet)に画像を1つずつ追加するために使用されます。
「changeImage」メソッドは、特定のボタンの背景を設定するために使用されます。
ボタンの背景として画像をランダムに設定することができますが、
しかし、主な問題は、固定の動的画像を特定のボタンに設定する必要があることです。
現在、特定のボタンをクリックするたびに、1回、2回、3回押すと、ランダムな画像が変更されます...
「changeImage」でランダムに生成された特定の画像をシングルボタンに設定し、他のボタンをシングルシングルに設定する必要があります。
次に、2つのボタンの背景が同じかどうかを確認する必要があります。そうすると、これら2つのボタンがマトリックスから削除されます。
私がしている間違いについて私を導き、私を助けてください。