– (void)viewDidLoad
{
[super viewDidLoad];
NSInteger left = 0;
for (NSInteger i=0; i<3; i++) {
testBtn = [[UIButton alloc]initWithFrame:CGRectMake(left, 0, 50, 50)];
testBtn.backgroundColor = [UIColor blueColor];
testBtn.tag=i;
[testBtn addTarget:self action:@selector(chicks:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:testBtn];
left += 60;
}
// Do any additional setup after loading the view, typically from a nib.
}
-(void)chicks:(UIButton *)btn{
NSLog(@”%i”,btn.tag);
}
(以上代码中 btn.tag = testBtn.tag)