我以编程方式向 UIView(通过 addSubview)添加了一些按钮。我在这个按钮上添加了一个带有函数的@selector。但是,它们出现在视图中,但是当我单击时,最后一个按钮仅起作用。
进入我的.h:
@property (nonatomic, strong) UIButton * myButton;
进入我的 .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);
}
}
如何将操作添加到所有按钮?不只是最后...