【问题标题】:how to pass multiple tags to addTarget:action:forControlEvents?如何将多个标签传递给 addTarget:action:forControlEvents?
【发布时间】:2014-03-04 12:46:15
【问题描述】:

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

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

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

【问题讨论】:

  • tag1tag2 是什么?
  • 你想用这两个标签达到什么目的?
  • 如果你想为不同的UIButton调用相同的IBAction方法,那么你需要比较IBAction方法中的标签。这是你要找的吗?
  • 你想要什么,可以请你更清楚地分享一下
  • 您是否只是想将 2 个标签分配给同一个按钮?你为什么要这样做?

标签: ios tags ibaction


【解决方案1】:

我得到了你的问题.. 你不能直接将两个标签分配给任何 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);
  }

我已经使用了几个大数字来对标签进行编码..希望它可以解决您分配两个标签的问题

【讨论】:

  • 我觉得好傻...真的很简单,加上两个想要的标签,然后从另一个中减去一个,再次得到两个标签...就像不使用交换两个整数的解决方案第三个变量
猜你喜欢
  • 2016-02-15
  • 2019-03-13
  • 1970-01-01
  • 2011-07-14
  • 1970-01-01
  • 2021-06-05
  • 2023-03-31
  • 1970-01-01
相关资源
最近更新 更多