Add a multiple buttons to a view programmatically, call the same method, determine which button it was

断了今生、忘了曾经 提交于 2019-11-27 03:34:59

You could either keep a reference to the actual button object somewhere that mattered (like an array) or set the button's tag to something useful (like an offset in some other data array). For example (using the tag, since this is generally must useful):

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

You can assign a tag to the button.

button.tag = i;

Then in -buttonClicked:, check the tag of the sender:

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

For that give different tag to each button & use code like this:

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

& in method

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

and so on to check which button is called

Akram Khan

If you want to add buttons at run time then there will be 10 20 50 or more than that. Then you should to use ui scroll view in this condition.

When the buttons will be generate then your scroll view size should be increased accordingly.

And you can write the code like this

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 has a tag property. Use that and in your buttonClicked method, you can check the button that was clicked based on it's tag. Might want to keep constants around for what button is what.

For each of your buttons set an appropriate tag, and then refer to the tag in your action. i.e.

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

You can easily set the tag based on the index of your iteration, if you're creating buttons in a loop. And then in your action:

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

Somebody might have this problem:

Jason Coco's answer worked fine for me, until I wanted to use the tag to access properties from an NSArray that was defined as a property.

Turns out the property had to be defined as "retain" instead of "weak".

The Swift version (with Labels):

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

Call using using self.viewWithTag(i) as UILabel:

(cell.viewWithTag(5) as UILabel).text
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!