各位我在做for循环button按钮的时候遇到这样的问题,我在button上放置了两个label和一个ImageView,我在通过button的tag来区分点击的哪个button,可是我在循环好button后不论点击哪个按钮执行效果的总是是最后一个button上显示,其它按钮上却并不显示这个是怎么回事呢?
for (int i=0; i<3; i++) {
button = [[UIButton alloc]init];
button.frame = CGRectMake(i*x, y, w, h);
images = [[UIImageView alloc]init];
eatImg = [[UIImageView alloc]init];
UILabel *labels = [[UILabel alloc]init];
numberLab = [[UILabel alloc]init];
labels.textAlignment = NSTextAlignmentCenter;
labels.textColor = [UIColor lightGrayColor];
labels.font = [UIFont systemFontOfSize:16.0f];
numberLab.textAlignment = NSTextAlignmentCenter;
numberLab.textColor = [UIColor lightGrayColor];
numberLab.font = [UIFont systemFontOfSize:16.0f];
if (kWindowHeight==568) {
labels.font = [UIFont systemFontOfSize:14.0f];
numberLab.font = [UIFont systemFontOfSize:14.0f];
}
if (i==0) {
eatImg.frame = CGRectMake(button.frame.size.width/4-10, 12, 20, 20);
[eatImg setImage:[UIImage imageNamed:@"想吃.png"]];
labels.frame = CGRectMake(button.frame.size.width/4+10, 5, button.frame.size.width-(button.frame.size.width/4+10), 20);
labels.text = @"想吃";
numberLab.frame = CGRectMake(button.frame.size.width/4+10, 25, button.frame.size.width-(button.frame.size.width/4+10), 20);
numberLab.text = @"88";
}else if (i==1){
images.frame = CGRectMake(button.frame.size.width/4-10, 12, 20, 20);
images.image = [UIImage imageNamed:@"评价.png"];
labels.frame = CGRectMake(button.frame.size.width/4+10, 5, button.frame.size.width-(button.frame.size.width/4+10), 20);
labels.text = @"评论";
numberLab.frame = CGRectMake(button.frame.size.width/4+10, 25, button.frame.size.width-(button.frame.size.width/4+10), 20);
numberLab.text = @"88";
}else if (i==2){
images.frame = CGRectMake(button.frame.size.width/4-10, 12, 20, 20);
images.image = [UIImage imageNamed:@"收藏.png"];
labels.frame = CGRectMake(button.frame.size.width/4+10, 5, button.frame.size.width-(button.frame.size.width/4+10), 20);
labels.text = @"收藏";
numberLab.frame = CGRectMake(button.frame.size.width/4+10, 25, button.frame.size.width-(button.frame.size.width/4+10), 20);
numberLab.text = @"88";
}
button.tag = i+1000;
[button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
[button addSubview:eatImg];
[button addSubview:images];
[button addSubview:labels];
[button addSubview:numberLab];
[footer addSubview:button];
}