以编程方式向视图添加多个按钮,调用相同的方法,确定它是哪个按钮

问题描述:

我想以编程方式向视图中添加多个UIButton - 在编译时未知按钮的数量。以编程方式向视图添加多个按钮,调用相同的方法,确定它是哪个按钮

我可以让一个或一个以上的UIButton的像这样(在一个循环,但短为简单起见):

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
[button addTarget:self 
     action:@selector(buttonClicked:) 
forControlEvents:UIControlEventTouchDown]; 
[button setTitle:@"Button x" forState:UIControlStateNormal]; 
button.frame = CGRectMake(100.0, 100.0, 120.0, 50.0); 
[view addSubview:button]; 

复制/编辑从这个链接: How do I create a basic UIButton programmatically?

但是我怎么确定buttonClicked:哪个按钮被点击了?如果可能的话我想传递标签数据来识别按钮。

您可以保留对实际按钮对象的引用(像数组一样)或将按钮的标记设置为有用的东西(如某些其他数据数组中的偏移量)。例如(使用标记,因为这通常是必须有用):

for(int i = 0; i < 5; i++) { 
    UIButton* aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    [aButton setTag:i]; 
    [aButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside]; 
    [aView addSubview:aButton]; 
} 

// then ... 

- (void)buttonClicked:(UIButton*)button 
{ 
    NSLog(@"Button %ld clicked.", (long int)[button tag]); 
} 
+0

非常感谢! – 2010-04-15 15:10:46

+1

a在某种意义上说??? .... – 2012-03-03 14:19:18

+5

按钮重叠。 – Jitendra 2013-07-01 11:24:02

您可以将标签分配给按钮。

button.tag = i; 

然后在-buttonClicked:,检查发件人的标签:

-(void)buttonClicked:(UIButton*)sender { 
    int tag = sender.tag; 
    ... 
} 
+0

非常感谢! – 2010-04-15 15:09:19

的UIButton有tag属性。使用它并在你的buttonClicked方法中,你可以检查被点击的按钮,它的标签。可能希望保持常量,以确定按钮是什么。

对于每个按钮设定适当的标记,然后参照在动作标签。即

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
... 
button.tag = 1 
[view addSubview:button]; 

如果你正在循环中创建按钮,你可以根据迭代索引轻松设置标签。然后在你的行动:

- (void)aButtonWasTapped:(UIButton *)source { 
    if(source.tag == 1) { 
     // etc 
    } 
} 

对于给不同的标签给每个按钮&使用这样的代码:

[btn1 addTarget:self action:@selector(pressbtn:) forControlEvents:UIControlEventTouchUpInside]; 
    [btn2 addTarget:self action:@selector(pressbtn:) forControlEvents:UIControlEventTouchUpInside]; 

&的方法

-(void)pressbtn:(id)sender { 
UIButton *button=sender; 
     if (button.tag==1) 
{ 
    NSLog(@"Press button 1"); 
} 
    if (button.tag==2) 
{ 
    NSLog(@"Press button 2"); 
} 

等检查哪些按钮被称为

如果你想添加按钮在运行时间则会有10 20 50或更多。那么你应该在这种情况下使用ui滚动视图。

当按钮将被生成时,您的滚动视图大小应相应增加。

,你可以写这样的

scrollview = [[UIScrollView alloc] init];  
     scrollview.contentSize = CGSizeMake(INVOICE_ADDITEM_SCROLLVIEW_CONTENT_WIDTH, INVOICE_ADDITEM_SCROLLVIEW_CONTENT_HEIGHT); 
     scrollview.frame = CGRectMake(0,50, SCROLLVIEW_WIDTH, SCROLLVIEW_HEIGHT); 
     // scrollview.backgroundColor = [UIColor whiteColor]; 
     scrollview.scrollsToTop = NO; 
     scrollview.delegate = self; 
     [self.view addSubview:scrollview]; 


    for (int pos = 0; pos < 2; pos++) { 
      UIButton *but = [UIButton buttonWithType:UIButtonTypeCustom]; 
      [but setImage:[UIImage imageNamed:@"checkbox.png"] forState:UIControlStateNormal]; 
      [but setImage:[UIImage imageNamed:@"checkbox_active.png"] forState:UIControlStateSelected]; 
      [but setFrame:CGRectMake(TXT_FLD_X_CORD+90, 295, 20, 20)]; 
      //   [but setCenter:CGPointMake(50, i*40+20)]; 
      but.tag = pos; 
      /*if(pos==0) 
      { 
      [but setImage:[UIImage imageNamed:@"checkbox_active.png"] forState:UIControlStateNormal]; 
      // [but setImage:[UIImage imageNamed:@"checkbox_active.png"] forState:UIControlStateSelected]; 
      }*/ 
      [but setCenter:CGPointMake(pos*90+125 ,310)]; 
      [but addTarget:self action:@selector(checkboxButton:) forControlEvents:UIControlEventTouchUpInside]; 
      [scrollview addSubview:but]; 
     } 

有人代码可能有这样的问题:

杰森Coco的答案对我来说工作得很好,直到我想从使用标签来访问属性一个被定义为属性的NSArray。

原来的属性必须定义为“保留”而不是“弱”。

雨燕版(带标签):使用使用

for index in 1...5 { 
     var label = UILabel() 
     label.tag = index 
     labelsDictionary["Label\(index)"] = label 
     self.addSubview(label) 
    } 

呼叫self.viewWithTag(I)为的UILabel:

(cell.viewWithTag(5) as UILabel).text