代码实现UIButton,添加点击事件,怎么能获取sender

2025-05-08 16:57:26
推荐回答(1个)
回答1:

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];
}
因为这是苹果定义好的接口,你只能照着来.
另外,有一个tag属性是可以记录些简单的信息的,就比如说你这里的index.