欲编程多个UIButtons添加到一个视图 - 按钮的数目在编译时未知

我可以使一个或更多个的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];

复制的/已编辑从这个链接: 如何以编程方式创建基本的UIButton?

但我怎么确定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]);
}

其他提示

可以一个标签分配给该按钮。

button.tag = i;

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

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

有关,让不同的标签,以像这样的每个按钮使用与代码:

[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或不止于此。那么你应该使用的用户界面在这种情况下滚动视图。

当的按钮将生成然后滚动视图尺寸应相应增加。

你可以这样写

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

的UIButton具有tag属性。使用在你的buttonClicked方法,你可以检查是根据它的标签单击该按钮。可能要保持周围常数哪个按钮是什么。

有关每个按钮的设置适当的标记,然后参照在动作标签。即。

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

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

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

有人可能有这样的问题:

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

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

在夫特版(带标签):

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

使用self.viewWithTag(ⅰ)作为使用的UILabel呼叫:

(cell.viewWithTag(5) as UILabel).text
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top