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

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

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

最佳答案

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

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

关于ios - 如何将多个标签传递给 addTarget :action:forControlEvents?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22172352/

相关文章:

iphone - 预定的 UILocalNotifications 是 iOS 设备备份的一部分吗?

git:仅推送特定标签

javascript - 将特殊标签应用于所有选定文本中的文本区域,即使有换行符捕获光标位置中的所有内容

ios - 如何为在类方法中创建的对象创建 IBAction

ios - 触发按钮以在 swift3 xcode 8 ios 10 中更改 UITextView 中的文本

ios - 使用 IBAction 选择并按下表格单元格行

ios - 如何通过 iOS 设备向本地主机发出请求(使用 Xcode)?

ios - SCNRenderer 不在 iOS 13 上渲染 AVPlayer

ios - 如何确保在文本字段中仅选择选择器数据

android - 在 Android 手机上模拟 NFC 标签