簡體   English   中英

如何將多個標簽傳遞給addTarget:action:forControlEvents?

[英]how to pass multiple tags to addTarget:action:forControlEvents?

我知道我寫的代碼是錯誤的。 但我想要這樣的東西。 怎么做?

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
int totalcout = 0;
int passValue ;
for(int j=0; j<5; j++{
    for(int i=0; i<5; i++)
    {
        totalcout++;
        if(totalcount >1){
          break;
        }else{
        passValue = i;
        }
    }
    button.tag = j;
    [button addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchUpInside];
}


- (IBAction) button:(UIButton *)sender {
    NSLog(@"tag numbers are  %d", sender.tag);

    detailViewController.mutableArray1 = [oneMutableArray objectAtIndex:sender.tag];
    detailViewController.mutableArray2 = [twoMutableArray objectAtIndex:passValue];
}

我希望問題清楚。 先感謝您

我有您的問題..您不能直接將兩個標簽直接分配給任何UIView或任何子類。 但您可以間接實現,也許這段代碼有助於實現最終獲得兩個標簽的意圖

    #define First_Tag  100
    #define Second_Tag 200
  -(void)createButton
     {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.tag = ((First_Tag*10000)+30000)+(Second_Tag*10);
   [button addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchUpInside];
      }

 - (void) button:(UIButton *)sender 
 {
   int intTag2 = ((sender.tag-30000)%10000)/10;
   int intTag1 = ((sender.tag-(intTag2*10))-30000)/10000;
   NSLog(@"tag numbers are  %d and %d", intTag1, intTag2);
  }

我使用了幾個大數字來編碼標簽..希望它可以解決您分配兩個標簽的問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM