如何将多个标签传递给addTarget:action:forControlEvents?

时间:2014-03-04 12:46:16

标签: ios tags ibaction

我知道我写的代码是错误的。但我想要这样的东西。怎么做?

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];
}

我希望问题很清楚。 提前谢谢

1 个答案:

答案 0 :(得分:3)

我收到了你的问题..你不能直接为任何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);
  }

我使用了几个大数字来编码标签..希望它能解决你的问题,分配两个标签