【问题标题】:Add a multiple buttons to a view programmatically, call the same method, determine which button it was以编程方式向视图添加多个按钮,调用相同的方法,确定它是哪个按钮
【发布时间】:2011-02-08 10:22:03
【问题描述】:

我想以编程方式将多个 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];

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

但是如何在 buttonClicked 中确定:单击了哪个按钮?如果可能的话,我想传递标签数据来识别按钮。

【问题讨论】:

    标签: iphone objective-c xcode programmatically-created


    【解决方案1】:

    您可以在重要的地方(如数组)保留对实际按钮对象的引用,也可以将按钮的标签设置为有用的东西(如其他数据数组中的偏移量)。例如(使用标签,因为这通常很有用):

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

    【讨论】:

    • 如果您想为每个按钮执行不同的操作怎么办?
    【解决方案2】:

    您可以为按钮分配标签。

    button.tag = i;
    

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

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

    【讨论】:

      【解决方案3】:

      【讨论】:

        【解决方案4】:

        为此,为每个按钮提供不同的标签并使用如下代码:

        [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");
        }
        

        等等来检查哪个按钮被调用

        【讨论】:

          【解决方案5】:

          如果您想在运行时添加按钮,那么将有 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];
                  }
          

          【讨论】:

            【解决方案6】:

            UIButton 有一个tag 属性。使用它并在您的 buttonClicked 方法中,您可以根据其标签检查单击的按钮。可能想要为什么按钮保留常量。

            【讨论】:

              【解决方案7】:

              为您的每个按钮设置一个适当的标签,然后在您的操作中引用该标签。即

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

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

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

              【讨论】:

                【解决方案8】:

                有人可能有这个问题:

                Jason Coco 的 回答对我来说效果很好,直到我想使用标记从定义为属性的 NSArray 中访问属性。

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

                【讨论】:

                  【解决方案9】:

                  Swift 版本(带标签):

                   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
                  

                  【讨论】:

                    猜你喜欢
                    • 2017-02-23
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 2013-07-17
                    • 1970-01-01
                    • 2012-06-20
                    • 1970-01-01
                    相关资源
                    最近更新 更多