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...
This works fine:
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.