0

I've added some buttons to an UIView (via addSubview) programmatically. I’ve added to this button an @selector with a function. However, they appear in the view but when I do click, the last button works only.

Into my .h:

@property (nonatomic, strong) UIButton * myButton;

Into my .m

for(int i=0;i<5;i++){

myButton = [UIButton buttonWithType: UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(55, 55*i, 30, 30);
myButton.tag = i;
myButton.backgroundColor = [UIColor redColor];
[myButton addTarget:self action:@selector(myaction:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:myButton];

}

-(void)myaction:(UIButton *)sender{
  if(sender.tag == 0){
    NSLog(@“uibutton clicked %ld", (long)sender.tag);
  }
}

How do I add the action to all buttons? not for the last only...

Rajesh Dharani
  • 285
  • 4
  • 20
user3745888
  • 6,143
  • 15
  • 48
  • 97
  • You are overwriting each time the same var? – Larme Feb 22 '18 at 16:54
  • So what's your issue then? Do you see all 5 buttons? Is `myaction` called for all 5 buttons? It should be based on the code you posted. – rmaddy Feb 22 '18 at 16:54
  • I can see all 5 buttons, but only when I click on the button number 5 it is called. Isn't called when I click on others buttons – user3745888 Feb 22 '18 at 16:57
  • There must be more going on than you are showing. One obvious bit of confusion, as Larme mentioned, is your strange use of the `myButton` property. Why do you have a single button property being used to create 5 buttons? Just use a local variable and get rid of that property. – rmaddy Feb 22 '18 at 17:01
  • show the whole code, there must be something interfering with the buttons – Milan Nosáľ Feb 22 '18 at 17:12
  • I tried this too @rmaddy , but is indiferent the use of UIButton *mybutton=[UIButton buttonWithType:UIButtonTypeCustom]; into the loop to declare the uibutton all times – user3745888 Feb 22 '18 at 17:14
  • Can you post real code? What you have doesn't compile. (e.g. Your use of `myButton` doesn't reference the property, `myButton = CGRectMake(55, 55*i, 30, 30)` doesn't make any sense, ....) – Phillip Mills Feb 22 '18 at 19:15
  • After fixing the errors I mentioned above, your code works fine for me. (Even better after deleting `if(sender.tag == 0){`.) – Phillip Mills Feb 22 '18 at 19:24

2 Answers2

0

This works fine:

- (void)viewDidLoad {
    [super viewDidLoad];

    for(int i=0;i<5;i++){

        UIButton *myButton = [UIButton buttonWithType: UIButtonTypeRoundedRect];
        myButton.frame = CGRectMake(55, 55*i, 30, 30);
        myButton.tag = i;
        myButton.backgroundColor = [UIColor redColor];
        [myButton setTitle:[NSString stringWithFormat:@"%ld", (long)i] forState:UIControlStateNormal];
        [myButton addTarget:self action:@selector(myaction:) forControlEvents:UIControlEventTouchUpInside];

        [self.view addSubview:myButton];
    }


}

-(void)myaction:(UIButton *)sender{
    NSLog(@"uibutton clicked %ld", (long)sender.tag);
}

The code you posted in your question appears to be "this is what my code looks like" rather than your actual code. That can cause problems when people try to help.

In the future, post your actual code.

DonMag
  • 69,424
  • 5
  • 50
  • 86
  • This is the same code (other than the button title) in the question. How does this solve the problem? – rmaddy Feb 22 '18 at 18:08
-1

Let's make it more dynamic by calculating the height of the button and adding padding based on the number of buttons.

:

-(void)viewDidLoad {

 [super viewDidLoad];

 NSInteger height = self.frame.size.height - 5*20; // 5 is the button count 
 and 20 is the padding 
 NSInteger buttonHeight = height/5;
 for(int i=0;i<5;i++){
    UIButton *myButton = [UIButton buttonWithType: UIButtonTypeRoundedRect];
    myButton.frame = CGRectMake(55, buttonHeight*i+padding*(i+1), 150, 
     buttonHeight);
    myButton.tag = i;
    myButton.backgroundColor = [UIColor redColor];
    [myButton setTitle:[NSString stringWithFormat:@"%ld", (long)i] 
    forState:UIControlStateNormal];
    [myButton addTarget:self action:@selector(myaction:) 
    forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:myButton];
}}

-(void)myaction:(UIButton *)sender{
 NSLog(@"uibutton clicked %ld", (long)sender.tag);
}

You could make it more dynamic by calculating the height of the button like this:

NSInteger height = self.frame.size.height - 5*20; 
NSInteger buttonHeight = height/5;
ymyh
  • 11
  • 6
  • Please don't post code without any explanation. A good answer explains what was wrong and how the answer solves the issue. – rmaddy Feb 22 '18 at 19:37