2012-05-31 80 views
4
-(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)一個接一個。

changeImage」方法用於設置特定按鈕的背景。

我能夠將圖像作爲背景隨機設定的按鈕,

但主要的問題是,我必須固定動態圖像設置爲特定的按鈕。

對特定按鈕的每次點擊我得到改變當我按下一次,兩次,三次等隨機圖像...

現在我必須設置這是在「changeImage隨機產生的任何特定圖像「以單按鈕&休息到其他按鈕單一。

然後我必須檢查兩個按鈕是否具有相同的背景,然後這兩個按鈕將從矩陣中刪除。

請指導我的錯誤我在做&幫助我。

+0

[sender setBackgroundImage:img forState:UIControlStateNormal]; [sender setEnabled:FALSE]; 現在圖像沒有改變...如果我寫了兩行....但它應該消失,如果沒有找到匹配圖像.... –

+0

更新:[發件人setBackgroundImage:img forState:UIControlStateDisabled];您可以看到該圖像但處於禁用狀態。 – Jasmit

+0

有什麼辦法可以產生隨機但均勻的圖像集(如「1_1.png」),它應該是2,4,6等....設置爲按鈕...? –

回答

0
//Please replace this method 

    -(void)changeImage:(UIButton *)sender 
    {  
      UIImage *img;  
      NSString *strImageName; 
      strImageName = [arr_FirstSet objectAtIndex:arc4random() % [arr_FirstSet count]/2];  

      img = [UIImage imageNamed:strImageName]; 

    //for the first time sender.imageView.image property will be null then only we set image to the button. 
//For second click this condition fails and does not set the other image. 
      if(!sender.imageView.image) 
      { 
       [sender setImage:img forState:UIControlStateHighlighted]; 
       [sender setImage:img forState:UIControlStateSelected]; 
      } 
// Calling a method to check if two images of buttons are same. 
      [self checkIfTwoImagesAreSame:sender]; 
    } 

    //And Add this method 

    - (void)checkIfTwoImagesAreSameImageMaching:(UIButton *)sender 
    { 
     for(UIView *subview in [viewWithButtons subviews]) 
     { 
      if ([subview isKindOfClass:[UIButton class]]) 
      { 
       UIButton *btn = (UIButton *)subview; 
// This condition is comparing clicked button image with all the buttons images of the viewWithButtons view if it isEqual: 
       if(sender.imageView.image && btn.imageView.image && [sender.imageView.image isEqual:btn.imageView.image]) 
       { 
// this is checking if the clicked button is not comparing with itself 
        if(btn.tag != sender.tag) 
        { 
         [btn removeFromSuperview]; 
         [sender removeFromSuperview]; 
    //     OR 
    //     btn.selected = YES; 
    //     sender.selected = YES; 
    //     OR      
    //     btn.hidden = YES; 
    //     sender.hidden = YES; 
        } 
       } 
      } 
     } 
    } 
+0

請解釋你的答案。 – Popeye

+0

我已將評論添加到答案。謝謝! –