for (int i =0; i<4; i++) {
btn =[[UIButton alloc]initWithFrame:CGRectMake(40+60*i, 100, 40, 40)];
btn.backgroundColor =[UIColor redColor];
btn.tag =100+i;
[btn addTarget:self action:@selector(qqqqq:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
因为btn =[UIButton alloc]init 相当于重新分配了一个地址,所以最后在试图上显示是创建了4个按钮。
btn =[[UIButton alloc]init];
for (int i =0; i<4; i++) {
btn.frame =CGRectMake(40+60*i, 100, 40, 40);
btn.backgroundColor =[UIColor redColor];
btn.tag =100+i;
[btn addTarget:self action:@selector(qqqqq:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
同类,一直使用的是同一个地址,所以只有一个button。