-(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];
This is my code for setting dynamic buttons in the setButtons_AsPerTheMatrixSelection method,
" AddImageToArray " is used to add images from a package to NSMutableArray (arr_FirstSet) one by one.
" changeImage " is used to set the background of a particular button.
I can set the images as the background for the buttons in random order,
But the main problem is that I have to set a fixed dynamic image to a specific button.
right now, on every click of a certain button, I get a modified random image when I click it once, twice, three times, etc.
I need to set some specific image that is randomly generated in "changeImage" on one button and the rest on the remaining buttons of a single single.
Then I have to check if the two buttons have the same background, after which these two buttons will be removed from the matrix.
I ask you, please, for the mistake I am making and helping me.