后来我发现,把clickHandler改成:带一个参数的函数,怎这个参数就默认是sender了?这是为什么?好神奇
buttonsArray = [NSMutableArray array];
for (int i = 0; i < 7; i ++) {
UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(i*30, 100, 20, 20)];
//...略去给button设置frame或者layer上的属性的代码
button.layer.borderColor = [[UIColor redColor] CGColor];
button.layer.borderWidth = 3;
[self.view addSubview:button];
[buttonsArray addObject:button];
[button addTarget:self action:@selector(click:)
forControlEvents:UIControlEventTouchUpInside];
}
-(void)click:(UIButton *)a{
int num = [buttonsArray indexOfObject:a];
NSLog(@"%d", num);
}